美文网首页
self-manager的使用.

self-manager的使用.

作者: small_Sea | 来源:发表于2017-01-13 15:35 被阅读0次
    由于最近较忙好久没写了,最近看了一种设计模式,"self-manager" 感觉特别爽,跟大多数人一样之前想把跳转写在view里但是没有找到办法,所以要回代理啊,block 传值, 学习了这种设计模式之后,没找到demo, 虽然看了sunnyxx 大神写的博客,但是我们要用起来的,所以写了一个小demo

    sunnyxx 介绍的已经很详细了,所以 直接转载了,在下面附上Demo

    51530583jw1ez4xxydguqj20k40a4ac0.jpg

    它的职责包括:

     -   通过传入的 URL,加载并展示头像图片
     -   显示一些附属信息,比如大V的标志
     -   将用户点击头像的事件传递给外层的 View Controller 跳转到用户信息页面
    

    于是乎这个 Widget 的 API 可以长这个样子:

      @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(@"页面跳转了====");
        TempViewController  \* vc = [[TempViewController alloc] init];
        [navigationController pushViewController:vc animated:YES];
    }];
    }
    
    原博客中提到 "实现时最好要调用 View 主类提供的 API:"

    如果不是调用主View 的话会有什么问题,暂时不清楚, 还有就是调用主View 的时候.uid 怎么传进来.

    源码参考

    相关文章

      网友评论

          本文标题:self-manager的使用.

          本文链接:https://www.haomeiwen.com/subject/gnlsbttx.html