-
ViewController基類
應(yīng)用開發(fā)的時候,總有一些ViewController是樣式相同的,那么就可以給這些樣式相同的ViewController創(chuàng)建一些基類,并且利用里面的viewDidLoad等函數(shù)完成一些重復(fù)的功能.出于自定義需求,我們還可以自定義一個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)];
//設(shè)置個背景色
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
-
子ViewController里換掉LeftBarButtonItem
如果我在自ViewController里換掉了LeftBarButtonItem,那我push進(jìn)入一個新的同樣基類的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進(jìn)行init的時候,又從新調(diào)用了基類的ViewDidLoad把LeftBarButtonItem換回來了.
//偽代碼
//堆棧調(diào)用順序
[ViewControllerOne viewDidLoad];
[ViewControllerOne setLeftBarButtonItemAsDismissBtn];
[ViewControllerOne pushToViewControllerTwo];
[[ViewControllerTwo alloc]init]
[ViewControllerOne viewDidLoad];
[BaseViewController viewDidLoad];
[BaseViewController setLeftBarButtonItemTarget:self action:@selector(popToPreview)];