控制器的创建方法&View的生命周期
很久没写总结了,很多东西已经写好了会慢慢上传滴。
今天先来点有趣的生命周期,自己也巩固了基础的知识,小白可以看看,大神不喜勿喷。
1.控制器的创建方法:
/**
* 控制器的创建方法
> 通过代码
> 通过xib
> 通过storyboard
*/
设置根控制器
1> 通过代码
ViewController *vc = [[ViewController alloc] init];
2> 通过xib创建
xib的作用:用来描述局部view的结构
参数1:NibName xib文件名
参数2:bundle:nil 表示mainBundle
initWithNibName:方法传入的xib作用使用来描述控制器view的结构
ViewController *vc = [[ViewController alloc] initWithNibName:@"View" bundle:nil];
3> 通过storyboard
// 加载SB
// 提示:下面代码仅仅是加载的stroyboard,并没有创建sb中的控制器
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// 创建箭头指向的控制器
UIViewController *vc = sb.instantiateInitialViewController;
// 通过标识加载控制器
// UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"xxx"];
2.简单说下loadView这个方法:
/*
Creates the view that the controller manages.
You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property.
If the view controller has an associated nib file, this method loads the view from the nib file. A view controller has an associated nib file if the nibName property returns a non-nil value, which occurs if the view controller was instantiated from a storyboard, if you explicitly assigned it a nib file using the initWithNibName:bundle: method, or if iOS finds a nib file in the app bundle with a name based on the view controller'��s class name. If the view controller does not have an associated nib file, this method creates a plain UIView object instead.
If you use Interface Builder to create your views and initialize the view controller, you must not override this method.
You can override this method in order to create your views manually. If you choose to do so, assign the root view of your view hierarchy to the view property. The views you create should be unique instances and should not be shared with any other view controller object. Your custom implementation of this method should not call super.
If you want to perform any additional initialization of your views, do so in the viewDidLoad method.
*/
/**
* 只要重写了该方法,不管是否有xib或者sb,都不会从xib或sb创建view
* loadView:用来自定义控制器的view
* 当控制器的view被访问时且为nil时,系统会自动调用该方法创建控制器view
*/
//- (void)loadView{
// // 千万不要调用父类的该方法
//// [super loadView];
//// NSLog(@"%@",self.view);
// self.view = [[UIView alloc] init];
// self.view.backgroundColor = [UIColor whiteColor];
//}
3.视图的生命周期方法:
/**
* 将要添加到父控件中 addSubView
*
*/
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willRemoveSubview:newSuperview];
NSLog(@"%s", __FUNCTION__);
}
/**
* 已经添加到父控件中
*/
- (void)didMoveToSuperview {
[super didMoveToSuperview];
NSLog(@"%s--%@", __FUNCTION__,self.superview);
}
/**
* 将要添加到窗口上
*/
- (void)willMoveToWindow:(UIWindow *)newWindow {
[super willMoveToWindow:newWindow];
}
/**
* 已经添加到窗口上
*/
- (void)didMoveToWindow {
[super didMoveToWindow];
NSLog(@"%s--%@", __FUNCTION__,self.window);
}
4.-initWithFrame/initWithCoder/awakeFromNib方法介绍:
/**
* 当对象从文件(xib/sb)中创建时调用(对象不仅仅局限UIView,任何OC对象)
* 当执行到该方法时,属性还没有跟xib/sb中的控件连好线
*/
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
NSLog(@"%s", __FUNCTION__);
}
return self;
}
/**
* 当对象从(xib/sb)中创建时调用(UIView) 当执行到该方法时,属性已经跟xib/sb中的控件连好线
*/
- (void)awakeFromNib {
NSLog(@"%s", __FUNCTION__);
[self setupUI];
}
/**
* 通过代码创建控件时,会调用 alloc init
*/
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
NSLog(@"%s", __FUNCTION__);
[self setupUI];
}
return self;
}
/**
* 专门布局子控件的frame
* 当控件frame发生变化时,系统自动调用,不能直接调用,一定要先调用父类
* 当往控件上添加子控件时也会触发
*/
- (void)layoutSubviews {
[super layoutSubviews];
NSLog(@"frame = %@",NSStringFromCGRect(self.frame));
}