美文网首页iOS工具方法
iOS链式编程(试写)

iOS链式编程(试写)

作者: M_PI_4 | 来源:发表于2020-03-27 02:31 被阅读0次

    RTButton.m

    #import "RTButton.h"
    
    @implementation RTButton
    
    - (instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        
        if(!self) {
            
        }
        
        return self;
    }
    
    - (RTButton * (^)(UIColor * color))btnNC {
        return ^(UIColor *color) {
            [self setTitleColor:color forState:UIControlStateNormal];
            return self;
        };
    }
    
    - (RTButton * _Nonnull (^)(UIColor * _Nonnull))btnSC {
        return ^(UIColor *color) {
            [self setTitleColor:color forState:UIControlStateSelected];
            return self;
        };
    }
    
    @end 
    

    ViewController.m

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor grayColor];
        
        RTButton *btn = [RTButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitle:@"Click Me" forState:UIControlStateNormal];
        btn.frame = CGRectMake(50, 100, 100, 60);
        btn.btnSC([UIColor redColor]).btnNC([UIColor blueColor]);
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:btn];
    }
    

    相关文章

      网友评论

        本文标题:iOS链式编程(试写)

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