很久沒寫總結(jié)了,很多東西已經(jīng)寫好了會(huì)慢慢上傳滴偷卧。
今天先來點(diǎn)有趣的生命周期熔吗,自己也鞏固了基礎(chǔ)的知識,小白可以看看愿卸,大神不喜勿噴灵临。
1.控制器的創(chuàng)建方法:
/**
*? 控制器的創(chuàng)建方法
> 通過代碼
> 通過xib
> 通過storyboard
*/
設(shè)置根控制器
1> 通過代碼
?ViewController *vc = [[ViewController alloc] init];
?2> 通過xib創(chuàng)建
?xib的作用:用來描述局部view的結(jié)構(gòu)
?參數(shù)1:NibName xib文件名
參數(shù)2:bundle:nil 表示mainBundle
?initWithNibName:方法傳入的xib作用使用來描述控制器view的結(jié)構(gòu)
? ?ViewController *vc = [[ViewController alloc] initWithNibName:@"View" bundle:nil];
?3> 通過storyboard
// 加載SB
// 提示:下面代碼僅僅是加載的stroyboard,并沒有創(chuàng)建sb中的控制器
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
// 創(chuàng)建箭頭指向的控制器
UIViewController *vc = sb.instantiateInitialViewController;
// 通過標(biāo)識加載控制器
//? ? UIViewController *vc =? [sb instantiateViewControllerWithIdentifier:@"xxx"];
2.簡單說下loadView這個(gè)方法:
/*
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,都不會(huì)從xib或sb創(chuàng)建view
*? loadView:用來自定義控制器的view
*? 當(dāng)控制器的view被訪問時(shí)且為nil時(shí),系統(tǒng)會(huì)自動(dòng)調(diào)用該方法創(chuàng)建控制器view
*/
//- (void)loadView{
//? ? // 千萬不要調(diào)用父類的該方法
////? ? [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__);
}
/**
*? 已經(jīng)添加到父控件中
*/
- (void)didMoveToSuperview {
[super didMoveToSuperview];
NSLog(@"%s--%@", __FUNCTION__,self.superview);
}
/**
*? 將要添加到窗口上
*/
- (void)willMoveToWindow:(UIWindow *)newWindow {
[super willMoveToWindow:newWindow];
}
/**
*? 已經(jīng)添加到窗口上
*/
- (void)didMoveToWindow {
[super didMoveToWindow];
NSLog(@"%s--%@", __FUNCTION__,self.window);
}
4.-initWithFrame/initWithCoder/awakeFromNib方法介紹:
/**
* 當(dāng)對象從文件(xib/sb)中創(chuàng)建時(shí)調(diào)用(對象不僅僅局限UIView,任何OC對象)
* 當(dāng)執(zhí)行到該方法時(shí),屬性還沒有跟xib/sb中的控件連好線
*/
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
NSLog(@"%s", __FUNCTION__);
}
return self;
}
/**
* 當(dāng)對象從(xib/sb)中創(chuàng)建時(shí)調(diào)用(UIView)? 當(dāng)執(zhí)行到該方法時(shí),屬性已經(jīng)跟xib/sb中的控件連好線
*/
- (void)awakeFromNib {
NSLog(@"%s", __FUNCTION__);
[self setupUI];
}
/**
* 通過代碼創(chuàng)建控件時(shí),會(huì)調(diào)用 alloc init
*/
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
NSLog(@"%s", __FUNCTION__);
[self setupUI];
}
return self;
}
/**
*? 專門布局子控件的frame
*? 當(dāng)控件frame發(fā)生變化時(shí),系統(tǒng)自動(dòng)調(diào)用,不能直接調(diào)用,一定要先調(diào)用父類
*? 當(dāng)往控件上添加子控件時(shí)也會(huì)觸發(fā)
*/
- (void)layoutSubviews {
[super layoutSubviews];
NSLog(@"frame = %@",NSStringFromCGRect(self.frame));
}