ViewController基类
应用开发的时候,总有一些ViewController是样式相同的,那么就可以给这些样式相同的ViewController创建一些基类,并且利用里面的viewDidLoad等函数完成一些重复的功能.出于自定义需求,我们还可以自定义一个LeftBarButtonItem.
//BaseViewController.m
#import "BaseViewController.h"
@implementation BaseViewController
- (void)popToPreview{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)setLeftBarButtonItemTarget:(id)target action:(SEL)action{
//自定义一个返回键
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *nImage = [UIImage resourceImageNamed:@"btn_back_white"];
[button setFrame:CGRectMake(0, 0, nImage.size.width, nImage.size.height)];
[button setBackgroundImage:nImage forState:UIControlStateNormal];
//自定义返回键功能
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
//添加到NavigationBar上
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
barBtnItem.style = UIBarButtonItemStylePlain;
[self.navigationItem setLeftBarButtonItem:barBtnItem];
}
#pragma mark - View Life Cycle
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
//自定义基类返回按键
[self setLeftBarButtonItemTarget:self action:@selector(popToPreview)];
//设置个背景色
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
子ViewController里换掉LeftBarButtonItem
如果我在自ViewController里换掉了LeftBarButtonItem,那我push进入一个新的同样基类的ViewController,返回键是那种情况呢
@interface ViewControllerOne : BaseViewController
@end
@interface ViewControllerTwo : BaseViewController
@end
//ViewControllerOne.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setLeftBarButtonItemAsDismissBtn];
}
- (void)setLeftBarButtonItemAsDismissBtn {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *nImage = [UIImage resourceImageNamed:@"btn_dismiss"];
[button setFrame:CGRectMake(0, 0, nImage.size.width, nImage.size.height)];
[button setBackgroundImage:nImage forState:UIControlStateNormal];
//
[button addTarget:self action:@selector(touchUpInsideDismissBtn:) forControlEvents:UIControlEventTouchUpInside];
//
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
barBtnItem.style = UIBarButtonItemStylePlain;
[self.navigationItem setLeftBarButtonItem:barBtnItem];
}
- (void)pushToViewControllerTwo {
ViewControllerTwo *vc = [[ViewControllerTwo alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}
答案是,返回键仍然保持基类的样式,第一次我遇见的时候有个疑问既然navigationItem是共享的,为什么我换掉后ViewControllerTwo没有保持我替换后的LeftBarButtonItem呢
没有被替换的原因
因为ViewControllerTwo进行init的时候,又从新调用了基类的ViewDidLoad把LeftBarButtonItem换回来了.
//伪代码
//堆栈调用顺序
[ViewControllerOne viewDidLoad];
[ViewControllerOne setLeftBarButtonItemAsDismissBtn];
[ViewControllerOne pushToViewControllerTwo];
[[ViewControllerTwo alloc]init]
[ViewControllerOne viewDidLoad];
[BaseViewController viewDidLoad];
[BaseViewController setLeftBarButtonItemTarget:self action:@selector(popToPreview)];