控制器的創(chuàng)建方法&View的生命周期

很久沒寫總結(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));

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市趴荸,隨后出現(xiàn)的幾起案子儒溉,更是在濱河造成了極大的恐慌,老刑警劉巖发钝,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件顿涣,死亡現(xiàn)場離奇詭異,居然都是意外死亡酝豪,警方通過查閱死者的電腦和手機(jī)涛碑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來寓调,“玉大人锌唾,你說我怎么就攤上這事《嵊ⅲ” “怎么了晌涕?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長痛悯。 經(jīng)常有香客問我余黎,道長,這世上最難降的妖魔是什么载萌? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任惧财,我火速辦了婚禮,結(jié)果婚禮上扭仁,老公的妹妹穿的比我還像新娘垮衷。我一直安慰自己,他們只是感情好乖坠,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布搀突。 她就那樣靜靜地躺著,像睡著了一般熊泵。 火紅的嫁衣襯著肌膚如雪仰迁。 梳的紋絲不亂的頭發(fā)上甸昏,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機(jī)與錄音徐许,去河邊找鬼施蜜。 笑死,一個(gè)胖子當(dāng)著我的面吹牛雌隅,可吹牛的內(nèi)容都是我干的翻默。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼恰起,長吁一口氣:“原來是場噩夢啊……” “哼冰蘑!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起村缸,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤祠肥,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后梯皿,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體仇箱,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年东羹,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了剂桥。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,919評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡属提,死狀恐怖权逗,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情冤议,我是刑警寧澤斟薇,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站恕酸,受9級特大地震影響堪滨,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蕊温,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一袱箱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧义矛,春花似錦发笔、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春量蕊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背艇挨。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工残炮, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人缩滨。 一個(gè)月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓势就,卻偏偏與公主長得像,于是被迫代替她去往敵國和親脉漏。 傳聞我的和親對象是個(gè)殘疾皇子苞冯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容