iOS Dev程序员ios实用开发技巧

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

2017-11-30  本文已影响149人  Suneday

本个章节进行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;
}

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

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

上一篇下一篇

猜你喜欢

热点阅读