Objective-C实现链式编程

2020-08-31  本文已影响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

上一篇下一篇

猜你喜欢

热点阅读