iOS项目开发框架“完全体”(三)-- 奥义“手里剑”

本个章节进行BaseViewController的讲解,并放出部分源码哦


首先我们在开发一个项目搭建基类框架的时候,BaseViewController是一个非常重要的环节,后面涉及到继承,重写等各种问题,我们该考虑设计的共有api又有哪些呢,下面开始啦

导航条文本的处理

  • 对应的接口
-(void)naviTitle:(NSString *)title color:(UIColor *)color font:(UIFont *)font;
-(JQBaseViewController *(^)(NSString *title))setNavTitle;
-(JQBaseViewController *(^)(UIColor *))setTitleColor;
-(JQBaseViewController *(^)(UIFont *))setTitleFont;
  • 对应的函数实现
-(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;
    };
}
  • 左右按钮的响应处理问题
  • 对应的接口
-(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;
  • 对应的函数实现
//左按钮
-(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函数添加一下代码
self.edgesForExtendedLayout = UIRectEdgeNone;

  • 内存警告的处理
-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    NSLog(@"⚠️%@出现内存警告⚠️", 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];
}
注:不做过多解释,相信大家都懂
  • 是否显示网络状态图标
//外部需要设置**bool变量来控制是否显示*
@property (assign, nonatomic) BOOL isOpen;
-(void)setIsOpen:(BOOL)isOpen
{
    _isOpen = _isOpen;
    [UIApplication sharedApplication].networkActivityIndicatorVisible = isOpen;
}
  • 检测登录(这里不做过多的说明了,一般都是判断session)
  • 各种权限的开启--举个例子吧,定位权限,手机相机,相册什么的,这里不一一不说明了
  • 分享面板的调用,由于项目不同需求,这里分享暂时不提供实例

BaseViewontroller涵盖了项目里面各个VC,以上函数足以满足大部分常用需求设置,如何自定义,即可自定在基类进行自定义添加...

谢谢阅读,有问题请咨询750460196@qq.com或者在评论区留言,再次感谢
有兴趣的话可以加入我的开发讨论群,可以在里面进行讨论,有问题我也可以进行解答,随时沟通,QQ 群:537916721

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 201,552评论 5 474
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,666评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,519评论 0 334
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,180评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,205评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,344评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,781评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,449评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,635评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,467评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,515评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,217评论 3 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,775评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,851评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,084评论 1 258
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,637评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,204评论 2 341

推荐阅读更多精彩内容