本個章節(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