self-manager的使用.

由于最近較忙好久沒寫了,最近看了一種設(shè)計(jì)模式,"self-manager" 感覺特別爽,跟大多數(shù)人一樣之前想把跳轉(zhuǎn)寫在view里但是沒有找到辦法,所以要回代理啊,block 傳值, 學(xué)習(xí)了這種設(shè)計(jì)模式之后,沒找到demo, 雖然看了sunnyxx 大神寫的博客,但是我們要用起來的,所以寫了一個(gè)小demo

sunnyxx 介紹的已經(jīng)很詳細(xì)了,所以 直接轉(zhuǎn)載了,在下面附上Demo

51530583jw1ez4xxydguqj20k40a4ac0.jpg

它的職責(zé)包括:

 -   通過傳入的 URL,加載并展示頭像圖片
 -   顯示一些附屬信息,比如大V的標(biāo)志
 -   將用戶點(diǎn)擊頭像的事件傳遞給外層的 View Controller 跳轉(zhuǎn)到用戶信息頁面

于是乎這個(gè) Widget 的 API 可以長這個(gè)樣子:

  @interface XHAvatarView : UIView
  - (void)configureWithAvatarURL:(NSURL \*)URL VIPInfo:(id)info tapped:(void (^)())block;
@end

@interface XHAvatarView (XHAvatarViewManager)
- (void)selfManagedConfigureWithAvatarURL:(NSURL \*)URL VIPInfo:(id)info uid:  (NSString \*)uid;
@end

#import "TempViewController.h"
@interface XHAvatarView ()
@property (nonatomic,copy) void (^tempBlcok)() ;
@end
@implementation XHAvatarView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
      if (self) {
          UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
          btn.frame = CGRectMake(10, 10, 50, 50);
          btn.backgroundColor = [UIColor redColor];
          [btn addTarget:self action:@selector(btndown) forControlEvents:UIControlEventTouchUpInside];
          [self addSubview:btn];  
       }
        return self;
}
- (void)btndown{
      if (self.tempBlcok) {
          self.tempBlcok();
      }
}

- (void)configureWithAvatarURL:(NSURL \*)URL VIPInfo:(id)info tapped:(void (^)())block{
    self.tempBlcok = block;
  // 賦值操作.
}
@end
@implementation XHAvatarView (XHAvatarViewManager)
- (void)selfManagedConfigureWithAvatarURL:(NSURL \*)URL VIPInfo:(id)info uid:(NSString \*)uid{
[self configureWithAvatarURL:URL VIPInfo:info tapped:^{
    UINavigationController \*navigationController= (id)UIApplication.sharedApplication.delegate.window.rootViewController;
    NSLog(@"頁面跳轉(zhuǎn)了====");
    TempViewController  \* vc = [[TempViewController alloc] init];
    [navigationController pushViewController:vc animated:YES];
}];
}
原博客中提到 "實(shí)現(xiàn)時(shí)最好要調(diào)用 View 主類提供的 API:"

如果不是調(diào)用主View 的話會(huì)有什么問題,暫時(shí)不清楚, 還有就是調(diào)用主View 的時(shí)候.uid 怎么傳進(jìn)來.

源碼參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容