舉例淺淡Block鏈?zhǔn)骄幊?/h2>

一. 簡述

在項(xiàng)目中, Block的鏈?zhǔn)骄幊炭隙ㄊ菚?huì)用到的. 如一些框架等.
而對(duì)于項(xiàng)目中一些自定義的工具或view, 在以下情況時(shí):

  • 1.可以包裝完整
  • 2.有多個(gè)屬性可以設(shè)置
  • 3.在項(xiàng)目中多處被用到

最好實(shí)現(xiàn)Block的鏈?zhǔn)骄幊? 如此對(duì)團(tuán)隊(duì)非常有好處.
注: block的鏈?zhǔn)? 即block有返回值, 且返回當(dāng)前對(duì)象

二.舉例

項(xiàng)目中需要自定義UINavigationBar, 符合上述3個(gè)條件.

  • 1.設(shè)置只讀的, 有返回值的block屬性
/** 設(shè)置下劃線 */
@property (nonatomic, copy, readonly) YHNavigationBarView *(^setupUnderline)(void);

///左按鈕圖片
@property (nonatomic, copy, readonly) YHNavigationBarView *(^leftBtnImage)(UIImage *image);
///左按鈕標(biāo)題
@property (nonatomic, copy, readonly) YHNavigationBarView *(^leftBtnTitle)(NSString *title);
///右按鈕圖片
@property (nonatomic, copy, readonly) YHNavigationBarView *(^rightBtnImage)(UIImage *image);
///右按鈕標(biāo)題
@property (nonatomic, copy, readonly) YHNavigationBarView *(^rightBtnTitle)(NSString *title);
///標(biāo)題按鈕圖片
@property (nonatomic, copy, readonly) YHNavigationBarView *(^titleBtnImage)(UIImage *image);
///標(biāo)題按鈕的標(biāo)題
@property (nonatomic, copy, readonly) YHNavigationBarView *(^titleBtnTitle)(NSString *title);
  • 2.重寫getter
  • 2.1
///設(shè)置下劃線
- (YHNavigationBarView *(^)(void))setupUnderline{
    return ^YHNavigationBarView *(void){
        
        //下劃線
        UIView *underlineView = [UIView new];
        underlineView.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        [self addSubview:underlineView];
        [underlineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.bottom.equalTo(self);
            make.height.equalTo(@1);
        }];
        
        return self;
    };
}
  • 2.2
///左按鈕圖片
- (YHNavigationBarView *(^)(UIImage *))leftBtnImage{
    return ^YHNavigationBarView *(UIImage *image){
        
        [self.leftBtn setImage:image forState:UIControlStateNormal];
        return self;
    };
}

其它getter方法與以上兩個(gè)方法類似, 就不浪費(fèi)篇章了.

  • 3.調(diào)用
  • 3.1 eg1
self.navBar.setupLeftBackBtn(self, YHBackImgTypeBlack).setupUnderline().
    setupRightPlayBtn(self, nil, navBarplayBtn, ^{
        
        [UIViewController yh_fromCurrentShowingVcToDestionVc:[YHPlayMusicVc new]];
    });
  • 3.2 eg2
self.navBar.setupLeftBackBtn(self, YHBackImgTypeBlack).
    setupTitleBtn(nil, @"修改昵稱", nil);
  • 3.3 注意循環(huán)引用
navBar.titleBtnAlpha(0).setupLeftBtn([UIImage imageNamed:@"nav_toolbar_back_white"], nil, ^{
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
        
    }).setupRightBtn([UIImage imageNamed:@"nav_share_white"], nil, ^{
        
        [weakSelf navRightButtonClick];
        
    }).setupTitleBtn(nil, self.titleStr, nil);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 轉(zhuǎn)載自:https://github.com/Tim9Liu9/TimLiu-iOS 目錄 UI下拉刷新模糊效果A...
    袁俊亮技術(shù)博客閱讀 12,146評(píng)論 9 105
  • 大一剛開學(xué),我便已經(jīng)有些厭倦,或者說,不安。 急吼吼地隨著如潮般的人流,起床、吃飯、自習(xí)、上課……我在各色人等中渺...
    一天一千字閱讀 400評(píng)論 0 0
  • 有些事情,在自己看來有意義就好了,無需去在意那些不肯定你付出的人。 ...
    穿毛褲的一七八閱讀 453評(píng)論 0 1

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