When to use awakeFromNib , initWithWindow , windowDidLoad methods?

First, you should never call awakeFromNib and windowDidLoad yourself. You can implement them in your custom classes, and then Cocoa will call them at the appropriate time.

awakeFromNib works for all objects loaded from a nib archive, not just windows and window controllers. It's a good general place to do setup—it's safe (you're guaranteed that the object is fully loaded, has returned from its init call, and has all of its outlets set), but still pretty early.

windowDidLoad (and windowWillLoad) works for all windows, whether loaded from a nib archive or created on the fly. But it's not called on the window, it's called on the window's controller. (Usually you're not creating your own NSWindow subclass, but you are creating your own NSWindowController subclass.) If you have setup code that depends on the window being loaded, you should put it here (but it's actually not that critical in the simple cases, because as soon as you try to access the window property, it will be created).

initWithWindow: is something you do call yourself, but a beginning Cocoa programmer probably doesn't ever want to do so.

You should probably read some of the guides that come with Xcode. If you want a document-based app, start with "Document-Based App Programming Guide for Mac". If you want a single-window utility app, you'll still need to learn about MVC and so on, so you might actually want to build a document-based app first to learn your way around.

Also, if you want to understand the sequence of events, override every message you can, and add something like NSLog(@"%s", __FUNCTION__); and your syslogs will reveal everything

 

第一,你應該從來沒有自稱 awakeFromNib 和 windowDidLoad。您可以在您自定義的類中實現它們,然後Cocoa將調用他們在適當的時間。

awakeFromNib 可用於從nib存檔加載,不只是窗口和窗口控制器的所有對象。它是一個很好,一般地方不要設置的程序 — — 它是安全的 (你要保障對象是完全加載、 已從其 init 調用、 返回和具有所有其出口設置),但仍然很早。

windowDidLoad (和 windowWillLoad) 適用於所有 windows 是否從nib存檔加載或創建動態。但它不是在窗口上,它稱爲窗口的控制器上。(通常你不去創造你自己的 NSWindow 子類,但您正在創建您自己的 NSWindowController 子類)。如果您有安裝程序代碼,取決於正在加載的窗口,你應該把它放在這裏 (但這是實際上不是關鍵在簡單情況下,因爲當您嘗試訪問的窗口屬性,將會創建它)。

initWithWindow: 是您確實調用了自己,但開始Cocoa程序員可能永遠不想這樣做。你大概應該讀一些與 Xcode 來的指南。如果您希望基於文檔的應用程序,從開始"基於文檔的應用程序編程指南爲 Mac"。如果你想要一個單窗口實用程序的應用程序,您仍然需要了解 MVC,等等,所以你可能會真正想要構建一個基於文檔的應用程序,首先要了解自己的方法。

此外,如果您想要了解的事件序列,可以重寫每個錯誤信息,比如添加 像NSLog(@"%s",__FUNCTION__);這樣你的系統日誌轉移將揭示的一切錯誤。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章