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;
}
复制代码