iOS項目開發(fā)框架“完全體”(三)-- 奧義“手里劍”

本個章節(jié)進行BaseViewController的講解省有,并放出部分源碼哦


首先我們在開發(fā)一個項目搭建基類框架的時候,BaseViewController是一個非常重要的環(huán)節(jié)幅疼,后面涉及到繼承盒粮,重寫等各種問題,我們該考慮設(shè)計的共有api又有哪些呢颖侄,下面開始啦

導航條文本的處理

  • 對應(yīng)的接口
-(void)naviTitle:(NSString *)title color:(UIColor *)color font:(UIFont *)font;
-(JQBaseViewController *(^)(NSString *title))setNavTitle;
-(JQBaseViewController *(^)(UIColor *))setTitleColor;
-(JQBaseViewController *(^)(UIFont *))setTitleFont;
  • 對應(yīng)的函數(shù)實現(xiàn)
-(void)naviTitle:(NSString *)title color:(UIColor *)color font:(UIFont *)font {
    self.navigationItem.title = title;
    [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName:color, NSFontAttributeName:font}];
}
-(JQBaseViewController *(^)(NSString *title))setupNaviTitle {
    return ^(NSString *title) {
        self.navigationItem.title = title;
        return self;
    };
}
-(JQBaseViewController *(^)(UIColor *))setupTitleColor {
    return ^(UIColor *color) {
        self.titleColor = color;
        [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName:self.titleColor, NSFontAttributeName:self.titleFont}];
        return self;
    };
}
-(JQBaseViewController *(^)(UIFont *))setupTitleFont {
    return ^(UIFont *font) {
        self.titleFont = font;
        [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName:self.titleColor, NSFontAttributeName:self.titleFont}];
        return self;
    };
}
  • 左右按鈕的響應(yīng)處理問題
  • 對應(yīng)的接口
-(void)leftItemTitle:(NSString *)title color:(UIColor *)color font:(UIFont *)font action:(void(^)())action;
-(JQBaseViewController *(^)(NSString *title, UIColor *color, UIFont *font, void(^action)()))setLeftTitleItem;
-(void)leftImageItem:(NSString *)imageName action:(void(^)())action;
-(JQBaseViewController *(^)(NSString *imageName, void(^action)()))setLeftImageItem;
-(void)leftItems:(NSArray *)items actions:(void(^)(NSInteger index))actions;
-(JQBaseViewController *(^)(NSArray *items,void (^actions)(NSInteger index)))setLeftItems;
-(void)rightItemTitle:(NSString *)title color:(UIColor *)color font:(UIFont *)font action:(void(^)())action;
-(JQBaseViewController *(^)(NSString *title, UIColor *color, UIFont *font, void(^action)()))setRightTitleItem;
-(void)rightImageItem:(NSString *)imageName action:(void(^)())action;
-(JQBaseViewController *(^)(NSString *imageName, void(^action)()))setRightImageItem;
-(void)rightItems:(NSArray *)items actions:(void(^)(NSInteger index))actions;
-(JQBaseViewController *(^)(NSArray *items,void (^actions)(NSInteger index)))setRightItems;
  • 對應(yīng)的函數(shù)實現(xiàn)
//左按鈕
-(void)leftItemTitle:(NSString *)title color:(UIColor *)color font:(UIFont *)font action:(void(^)())action {
    self.leftItemActionBlock = action;
    UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [itemButton setTitle:title forState:UIControlStateNormal];
    [itemButton setTitleColor:color forState:UIControlStateNormal];
    [itemButton.titleLabel setFont:font];
    [itemButton sizeToFit];
    [itemButton addTarget:self action:@selector(leftItemAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:itemButton];
    self.navigationItem.leftBarButtonItem = item;
}
-(JQBaseViewController *(^)(NSString *title, UIColor *color, UIFont *font, void(^action)()))setLeftTitleItem {
    return ^(NSString *title, UIColor *color, UIFont *font, void(^action)()){
        self.leftItemActionBlock = action;
        UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [itemButton setTitle:title forState:UIControlStateNormal];
        [itemButton setTitleColor:color forState:UIControlStateNormal];
        [itemButton.titleLabel setFont:font];
        [itemButton sizeToFit];
        [itemButton addTarget:self action:@selector(leftItemAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:itemButton];
        self.navigationItem.leftBarButtonItem = item;
        return self;
    };
}
-(void)leftImageItem:(NSString *)imageName action:(void(^)())action{
    self.leftItemActionBlock = action;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = item;
}
-(JQBaseViewController *(^)(NSString *imageName, void(^action)()))setLeftImageItem {
    return ^(NSString *imageName, void(^action)()){
        self.leftItemActionBlock = action;
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
        self.navigationItem.leftBarButtonItem = item;
        return self;
    };
}
-(void)leftItems:(NSArray *)items actions:(void(^)(NSInteger index))actions {
    self.leftItemsActionBlock = actions;
    NSMutableArray *barButtonItems = [NSMutableArray array];
    [items enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:obj];
        obj.tag = idx;
        [obj addTarget:self action:@selector(leftItemAction:) forControlEvents:UIControlEventTouchUpInside];
        [barButtonItems addObject:item];
        if (obj.frame.size.width == 0) {
            [obj sizeToFit];
        }
    }];
    self.navigationItem.leftBarButtonItems = barButtonItems;
}
- (JQBaseViewController *(^)(NSArray *items,void (^actions)(NSInteger index)))setupLeftItems {
    return ^(NSArray *items,void (^actions)(NSInteger index)){
        self.leftItemsActionBlock = actions;
        NSMutableArray *barButtonItems = [NSMutableArray array];
        [items enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:obj];
            obj.tag = idx;
            [obj addTarget:self action:@selector(leftItemAction:) forControlEvents:UIControlEventTouchUpInside];
            [barButtonItems addObject:item];
            if (obj.frame.size.width == 0) {
                [obj sizeToFit];
            }
        }];
        self.navigationItem.leftBarButtonItems = barButtonItems;
        return self;
    };
}
//右按鈕
-(void)rightItemTitle:(NSString *)title color:(UIColor *)color font:(UIFont *)font action:(void(^)())action {
    self.rightItemActionBlock = action;
    UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [itemButton setTitle:title forState:UIControlStateNormal];
    [itemButton setTitleColor:color forState:UIControlStateNormal];
    [itemButton.titleLabel setFont:font];
    [itemButton sizeToFit];
    [itemButton addTarget:self action:@selector(rightItemAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:itemButton];
    self.navigationItem.rightBarButtonItem = item;
}
- (JQBaseViewController *(^)(NSString *title, UIColor *color, UIFont *font, void(^action)()))setRightTitleItem {
    return ^(NSString *title, UIColor *color, UIFont *font, void(^action)()) {
        self.rightItemActionBlock = action;
        return self;
    };
}
-(void)rightImageItem:(NSString *)imageName action:(void(^)())action {
    self.rightItemActionBlock = action;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction:)];
    self.navigationItem.rightBarButtonItem = item;
}
-(JQBaseViewController *(^)(NSString *imageName, void(^action)()))setupRightImageItem {
    return ^(NSString *imageName, void(^action)()){
        self.rightItemActionBlock = action;
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction:)];
        self.navigationItem.rightBarButtonItem = item;
        return self;
    };
}
-(void)rightItems:(NSArray *)items actions:(void(^)(NSInteger index))actions{
    self.rightItemsActionBlock  = actions;
    NSMutableArray *barButtonItems = [NSMutableArray array];
    [items enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:obj];
        obj.tag = idx;
        [obj addTarget:self action:@selector(rightItemAction:) forControlEvents:UIControlEventTouchUpInside];
        [barButtonItems addObject:item];
        if (obj.frame.size.width == 0) {
            [obj sizeToFit];
        }
    }];
    self.navigationItem.rightBarButtonItems = barButtonItems;
}
-(JQBaseViewController *(^)(NSArray *items,void (^actions)(NSInteger index)))setRightItems {
    return ^(NSArray *items,void (^actions)(NSInteger index)){
        self.rightItemsActionBlock = actions;
        NSMutableArray *barButtonItems = [NSMutableArray array];
        [items enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:obj];
            obj.tag = idx;
            [obj addTarget:self action:@selector(rightItemAction:) forControlEvents:UIControlEventTouchUpInside];
            [barButtonItems addObject:item];
            if (obj.frame.size.width == 0) {
                [obj sizeToFit];
            }
        }];
        self.navigationItem.rightBarButtonItems = barButtonItems;
        return self;
    };
}
  • 間距處理問題

請在viewDidLoad函數(shù)添加一下代碼
self.edgesForExtendedLayout = UIRectEdgeNone;

  • 內(nèi)存警告的處理
-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    NSLog(@"??%@出現(xiàn)內(nèi)存警告??", NSStringFromClass([self class]));
}
  • 對象釋放情況的查看
-(void)dealloc
{
    NSLog(@"%@對象釋放", NSStringFromClass([self class]));
}
  • 自定義加載框
    注:這里是個別項目的自定義加載中樣式鸟雏,如何自定義享郊,可在基類里面自行添加
-(JQToastWindow *)loadingToastView
{
    if (!_loadingToastView) {
        _loadingToastView  = XIB(JQToastWindow);
        _loadingToastView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
    }
    return _loadingToastView;
}
  • 第三方加載框
-(void)mbshowHudOnApplicationKeyWindow
{
    [[MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES]setLabelText:@"正在加載..."];
}
-(void)mbhideHud
{
    [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
}
-(void)svpshowHudOnApplicationKeyWindow
{
    [SVProgressHUD showWithStatus:@"正在加載..."];
}
-(void)svphideHud
{
    [SVProgressHUD dismiss];
}
注:不做過多解釋,相信大家都懂
  • 是否顯示網(wǎng)絡(luò)狀態(tài)圖標
//外部需要設(shè)置**bool變量來控制是否顯示*
@property (assign, nonatomic) BOOL isOpen;
-(void)setIsOpen:(BOOL)isOpen
{
    _isOpen = _isOpen;
    [UIApplication sharedApplication].networkActivityIndicatorVisible = isOpen孝鹊;
}
  • 檢測登錄(這里不做過多的說明了炊琉,一般都是判斷session)
  • 各種權(quán)限的開啟--舉個例子吧,定位權(quán)限,手機相機苔咪,相冊什么的锰悼,這里不一一不說明了
  • 分享面板的調(diào)用,由于項目不同需求团赏,這里分享暫時不提供實例

BaseViewontroller涵蓋了項目里面各個VC箕般,以上函數(shù)足以滿足大部分常用需求設(shè)置,如何自定義舔清,即可自定在基類進行自定義添加...

謝謝閱讀丝里,有問題請咨詢750460196@qq.com或者在評論區(qū)留言,再次感謝
有興趣的話可以加入我的開發(fā)討論群体谒,可以在里面進行討論杯聚,有問題我也可以進行解答,隨時溝通抒痒,QQ 群:537916721

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末幌绍,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子故响,更是在濱河造成了極大的恐慌傀广,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件彩届,死亡現(xiàn)場離奇詭異主儡,居然都是意外死亡,警方通過查閱死者的電腦和手機惨缆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進店門糜值,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人坯墨,你說我怎么就攤上這事寂汇。” “怎么了捣染?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵骄瓣,是天一觀的道長。 經(jīng)常有香客問我耍攘,道長榕栏,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任蕾各,我火速辦了婚禮扒磁,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘式曲。我一直安慰自己妨托,他們只是感情好缸榛,可當我...
    茶點故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著兰伤,像睡著了一般内颗。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上敦腔,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天均澳,我揣著相機與錄音,去河邊找鬼符衔。 笑死负懦,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的柏腻。 我是一名探鬼主播纸厉,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼五嫂!你這毒婦竟也來了颗品?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤沃缘,失蹤者是張志新(化名)和其女友劉穎躯枢,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體槐臀,經(jīng)...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡锄蹂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了水慨。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片得糜。...
    茶點故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖晰洒,靈堂內(nèi)的尸體忽然破棺而出朝抖,到底是詐尸還是另有隱情,我是刑警寧澤谍珊,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布治宣,位于F島的核電站,受9級特大地震影響砌滞,放射性物質(zhì)發(fā)生泄漏侮邀。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一贝润、第九天 我趴在偏房一處隱蔽的房頂上張望绊茧。 院中可真熱鬧,春花似錦题暖、人聲如沸按傅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽唯绍。三九已至,卻和暖如春枝誊,著一層夾襖步出監(jiān)牢的瞬間况芒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工叶撒, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留绝骚,地道東北人。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓祠够,卻偏偏與公主長得像压汪,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子古瓤,可洞房花燭夜當晚...
    茶點故事閱讀 43,472評論 2 348

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,745評論 25 707
  • 秋風秋雨秋夜情止剖, 庭院深藏美嬌娘。 隔紗依依看嬌容落君, 此夕何夕再續(xù)情穿香。
    撒哈拉沙漠的熱風閱讀 229評論 1 0
  • 聽朋友推薦皮获,簡書是一個不錯的APP。在上班空閑的時候纹冤,我打開了網(wǎng)頁版的簡書洒宝。干凈的設(shè)計,給人的第一印象很好萌京。我是一...
    遇見同一個自己閱讀 286評論 0 0
  • 一聲兄弟一生情 不說幸運待德,一切都是命中注定。 ――題記 ...
    壹次心的星22閱讀 270評論 0 1
  • 今天我的心情非常難過枫夺!因為所有的人當中沒有能理解本人的內(nèi)心?就象下圖一樣酸敖堋!1 傳説中愛情是那么美好橡庞!可是誰愛誰...
    AAA山里紅閱讀 97評論 0 3