Xib生命周期初探

~~~~~~
First :將引用和內(nèi)容文件從xib文件中加載到內(nèi)存中
initWithCoder:
Seconded:將outlet中制定的各個(gè)新屬性
setValue:forKey:
Third: 將xib中定制的手勢(shì)交互等方法進(jìn)行處理
addTarget:action:forControlEvents:
Forth:連接對(duì)象
bind:toObject:withKeyPath:options:
Five:發(fā)送消息給《interface objects》通知加載完畢
awakeFromNib

歡迎指正在辆!

官方文檔原文如下:
1.It loads the contents of the nib file and any referenced resource files into memory:
The raw data for the entire nib object graph is loaded into memory but is not unarchived.
Any custom image resources associated with the nib file are loaded and added to the Cocoa image cache; see “About Image and Sound Resources.”
Any custom sound resources associated with the nib file are loaded and added to the Cocoa sound cache; see “About Image and Sound Resources.”
2.It unarchives the nib object graph data and instantiates the objects. How it initializes each new object depends on the type of the object and how it was encoded in the archive. The nib-loading code uses the following rules (in order) to determine which initialization method to use.
a. By default, objects receive an initWithCoder: message.
In OS X, the list of standard objects includes the views, cells, menus, and view controllers that are provided by the system and available in the default Xcode library. It also includes any third-party objects that were added to the library using a custom plug-in. Even if you change the class of such an object, Xcode encodes the standard object into the nib file and then tells the archiver to swap in your custom class when the object is unarchived.
In iOS, any object that conforms to the NSCoding protocol is initialized using the initWithCoder: method. This includes all subclasses of UIView and UIViewController whether they are part of the default Xcode library or custom classes you define.
b. Custom views in OS X receive an initWithFrame: message.
Custom views are subclasses of NSView for which Xcode does not have an available implementation. Typically, these are views that you define in your application and use to provide custom visual content. Custom views do not include standard system views (like NSSlider) that are part of the default library or part of an integrated third-party plug-in.
When it encounters a custom view, Xcode encodes a special NSCustomView object into your nib file. The custom view object includes the information it needs to build the real view subclass you specified. At load time, the NSCustomView object sends an alloc and initWithFrame: message to the real view class and then swaps the resulting view object in for itself. The net effect is that the real view object handles subsequent interactions during the nib-loading process.
Custom views in iOS do not use the initWithFrame: method for initialization.
c. Custom objects other than those described in the preceding steps receive an init message.

  1. It reestablishes all connections (actions, outlets, and bindings) between objects in the nib file. This includes connections to File’s Owner and other placeholder objects. The approach for establishing connections differs depending on the platform:
    ? Outlet connections
    In OS X, the nib-loading code tries to reconnect outlets using the object’s own methods first. For each outlet, Cocoa looks for a method of the form setOutletName: and calls it if such a method is present. If it cannot find such a method, Cocoa searches the object for an instance variable with the corresponding outlet name and tries to set the value directly. If the instance variable cannot be found, no connection is created.
    In OS X v10.5 and later, setting an outlet also generates a key-value observing (KVO) notification for any registered observers. These notifications may occur before all inter-object connections are reestablished and definitely occur before any awakeFromNib methods of the objects have been called. Prior to v10.5, these notifications are not generated. For more information about KVO notifications, see Key-Value Observing Programming Guide.
    In iOS, the nib-loading code uses the setValue:forKey: method to reconnect each outlet. That method similarly looks for an appropriate accessor method and falls back on other means when that fails. For more information about how this method sets values, see its description in NSKeyValueCoding Protocol Reference.
    Setting an outlet in iOS also generates a KVO notification for any registered observers. These notifications may occur before all inter-object connections are reestablished and definitely occur before any awakeFromNib methods of the objects have been called. For more information about KVO notifications, see Key-Value Observing Programming Guide.

? Action connections
In OS X, the nib-loading code uses the source object’s setTarget: and setAction: methods to establish the connection to the target object. If the target object does not respond to the action method, no connection is created. If the target object is nil, the action is handled by the responder chain.
In iOS, the nib-loading code uses the addTarget:action:forControlEvents: method of the UIControl object to configure the action. If the target is nil, the action is handled by the responder chain.
? Bindings
In OS X, Cocoa uses the bind:toObject:withKeyPath:options: method of the source object to create the connection between it and its target object.
Bindings are not supported in iOS.

  1. It sends an awakeFromNib message to the appropriate objects in the nib file that define the matching selector:
    ? In OS X, this message is sent to any interface objects that define the method. It is also sent to the File’s Owner and any placeholder objects that define it as well.
    ? In iOS, this message is sent only to the interface objects that were instantiated by the nib-loading code. It is not sent to File’s Owner, First Responder, or any other placeholder objects.
  2. It displays any windows whose “Visible at launch time” attribute was enabled in the nib file.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌漂坏,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件论悴,死亡現(xiàn)場(chǎng)離奇詭異孔厉,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)钧嘶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)棠众,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事闸拿】张危” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵新荤,是天一觀的道長(zhǎng)揽趾。 經(jīng)常有香客問(wèn)我,道長(zhǎng)苛骨,這世上最難降的妖魔是什么篱瞎? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮痒芝,結(jié)果婚禮上俐筋,老公的妹妹穿的比我還像新娘。我一直安慰自己严衬,他們只是感情好澄者,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著请琳,像睡著了一般粱挡。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上俄精,一...
    開(kāi)封第一講書(shū)人閱讀 52,457評(píng)論 1 311
  • 那天询筏,我揣著相機(jī)與錄音,去河邊找鬼嘀倒。 笑死屈留,一個(gè)胖子當(dāng)著我的面吹牛局冰,可吹牛的內(nèi)容都是我干的测蘑。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼康二,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼碳胳!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起沫勿,我...
    開(kāi)封第一講書(shū)人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤挨约,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后产雹,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體诫惭,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年蔓挖,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了夕土。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖怨绣,靈堂內(nèi)的尸體忽然破棺而出角溃,到底是詐尸還是另有隱情,我是刑警寧澤篮撑,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布减细,位于F島的核電站,受9級(jí)特大地震影響赢笨,放射性物質(zhì)發(fā)生泄漏未蝌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一茧妒、第九天 我趴在偏房一處隱蔽的房頂上張望树埠。 院中可真熱鬧,春花似錦嘶伟、人聲如沸怎憋。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)绊袋。三九已至,卻和暖如春铸鹰,著一層夾襖步出監(jiān)牢的瞬間癌别,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工蹋笼, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留展姐,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓剖毯,卻偏偏與公主長(zhǎng)得像圾笨,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子逊谋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

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

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,493評(píng)論 5 6
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,576評(píng)論 0 23
  • 一個(gè)人長(zhǎng)的不好看擂达,就理應(yīng)承受孤獨(dú)嗎? 如果所有的人都以長(zhǎng)相評(píng)判胶滋,世界上又缺少了多少可愛(ài)的偶像板鬓。 最近很忙,但是我仍...
    只求清平樂(lè)閱讀 229評(píng)論 0 0
  • 2017年1月12日 早晨7:30鬧鐘吵給不停究恤,把我從不滿意的睡眠中鬧醒俭令,我伸手使勁拍打了幾下鬧鐘,幾秒后鬧鐘停止...
    齊一文閱讀 541評(píng)論 12 4
  • 夾沙甜肉父玄德部宿,上庸三郡拒關(guān)求抄腔。 撕書(shū)斬使戰(zhàn)孟達(dá),兵敗成都賜死人。 【2016妓柜,9箱季,30。三古月南】
    三古月南閱讀 328評(píng)論 0 0