xcode 何时使用awakeFromNib、initWithWindow、windowDidLoad 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10886913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use awakeFromNib , initWithWindow , windowDidLoad methods?
提问by Gaurav_soni
Can you please explain me when we have to use the methods ? and also in what sort of situation should we use which method ? Right now i test which works and use that (yea i am a beginner).. Thanks. I am developing for MAC.
当我们必须使用这些方法时,你能解释一下吗?以及在什么样的情况下我们应该使用哪种方法?现在我测试哪个有效并使用它(是的,我是初学者)..谢谢。我正在为 MAC 开发。
回答by abarnert
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 和windowDidLoad。你可以在你的自定义类中实现它们,然后 Cocoa 会在适当的时候调用它们。
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.
awakeFromNib 适用于从 nib 存档加载的所有对象,而不仅仅是窗口和窗口控制器。这是一个进行设置的好地方——它是安全的(你可以保证对象已经完全加载,已经从它的 init 调用返回,并且设置了它的所有出口),但还很早。
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).
windowDidLoad(和 windowWillLoad)适用于所有窗口,无论是从 nib 存档加载还是动态创建。但它不是在窗口上调用,而是在窗口的控制器上调用。(通常你不是在创建你自己的 NSWindow 子类,而是创建你自己的 NSWindowController 子类。)如果你有依赖于正在加载的窗口的设置代码,你应该把它放在这里(但它实际上并不是那么重要的简单情况,因为只要您尝试访问 window 属性,它就会被创建)。
initWithWindow: is something you do call yourself, but a beginning Cocoa programmer probably doesn't ever want to do so.
initWithWindow: 是你自己称呼自己的东西,但是 Cocoa 初学者可能永远不想这样做。
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.
您可能应该阅读一些 Xcode 附带的指南。如果您想要基于文档的应用程序,请从“Mac 的基于文档的应用程序编程指南”开始。如果您想要一个单窗口实用程序应用程序,您仍然需要了解 MVC 等,因此您实际上可能希望首先构建一个基于文档的应用程序来学习您的方法。
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.
此外,如果您想了解事件的顺序,可以覆盖每条消息,并添加类似 NSLog(@"%s", __FUNCTION__); 的内容。你的系统日志将揭示一切。
回答by abodetheinc
During NIB unarchiving initWithWindow
gets called just after your window is unarchived from NIB. This is the place where your Window Controller gets initialised. Once entire unarchiving process finishes - means your window controller and UIView elements are ready - awakeFromNib
is called. Before this point either you don't have windowController or entire archiving process is not completed.
在initWithWindow
您的窗口从 NIB 取消存档后,NIB 取消存档会被调用。这是您的窗口控制器被初始化的地方。一旦整个取消归档过程完成 - 意味着您的窗口控制器和 UIView 元素已准备就绪 - 将awakeFromNib
被调用。在此之前要么你没有 windowController 要么整个归档过程没有完成。
When you do [label setStingValue]
in awakeFromNib
your window controller is ready moreover your view object's and associated context is ready.
当你[label setStingValue]
在awakeFromNib
你的窗口控制器准备好时,你的视图对象和关联的上下文也准备好了。