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