美文网首页
Objective-C实现链式编程

Objective-C实现链式编程

作者: 刘铁崧 | 来源:发表于2020-08-31 16:46 被阅读0次
    #import "CYViewController.h"
    
    @interface CYViewController ()
    @property (nonatomic,copy,readonly) CYViewController *(^funcA)(NSString *str);
    @property (nonatomic,copy,readonly) CYViewController *(^setColor)(UIColor *color);
    @property (nonatomic,strong)UILabel *testLabel;
    
    @end
    
    @implementation CYViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        _testLabel = [UILabel new];
        [_testLabel setBackgroundColor:[UIColor darkGrayColor]];
        [_testLabel setFrame:CGRectMake(20, 100, 200, 40)];
        [self.view addSubview:_testLabel];
    
        // 链式调用
        self.funcA(@"哈哈哈").setColor([UIColor whiteColor]);
    }
    
    - (CYViewController *(^)(UIColor *))setColor{
        return ^(UIColor *color){
            self.testLabel.textColor = color;
            return self;
        };
    }
    - (CYViewController *(^)(NSString *))funcA{
        return ^(NSString *str){
            [self.testLabel setText:str];
            return self;
        };
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:Objective-C实现链式编程

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