initWithNibName:bundle:
,designated初始化方法一個不符合規範的案例,會致使錯誤。web
//first vc
+ (instancetype)initWithUrl:(NSString *)url {
ViewController *controller = [ViewController new]; //已經在next vc的init中執行了viewDidLoad,而此時url尚未傳過去
controller.url = url;
return controller;
}
//next vc
#pragma mark - life cycle
- (instancetype)init {
self = [super init];
if(self) {
[self.view addSubview: self.webView]; //應該寫在viewDidLoad中
}
return self;
}
- (void)viewDidLoad {
//下面兩句應該寫在viewWillAppear:中
[self startLoading];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
}
複製代碼
好比頭條,上面有一個橫拉的欄目View,下面纔是ChildVC的view 作法:ScrollView + VCsobjective-c
注意數組
addChildViewController
後,childVC的生命週期方法,如viewWillAppear、viewDidAppear等,就跟隨父VC了自動處理。優化:緩存
/添加一個 childViewController
UIViewController *vc = [UIViewController new];
[self addChildViewController:vc];
vc.view.frame = ..;
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];
//移除一個 childViewController
[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];
複製代碼
實現相冊瀏覽功能,圖片放縮bash
#pragma mark - statusbar
-(BOOL)prefersStatusBarHidden {
return YES;
}
複製代碼