您的位置:首页 > 其它

When to use awakeFromNib , initWithWindow , windowDidLoad methods?

2014-05-05 15:28 429 查看
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__);这样你的系统日志转移将揭示的一切错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息