When is the NSApp's keyWindow created?
When does the keyWindow get created?
I thought the NSWindow would be created before the corresponding view controller's updateView method was called (Which I call in response to awakeFromNib), however if I create an alert sheet using NSApp's keyWindow, it does not appear correctly.
If I place a button on that view, however, and then bring up the alert when the user clicks on it, the keyWindow is defined, and the alert displays correctly (as expected).
My Application Delegate is almost completely empty.
I don't act开发者_高级运维ually want to display the alert at startup, but I do want to know when the key window is set up. :)
When does the keyWindow get created?
-[NSApp keyWindow]
points to an existing window (e.g. a window that has already been loaded from a nib file) that is currently the key window, typically by sending it -makeKeyAndOrderFront:
.
When an application starts, Cocoa:
- Loads the main nib file;
- Unarchives the contents of the nib file and instantiates its objects;
- Reestablishes the connections defined in the nib file;
- Sends
-awakeFromNib
to (a subset of) the nib file objects; - Displays windows that have been marked as Visible at launch time;
as described in the Resource Programming Guide.
If the nib file contains a single window, that window becomes key upon being shown provided it can become a key window, and this happens after -awakeFromNib
has been sent.
Also, the documentation for -[NSApplication keyWindow]
states that:
This method might return
nil
if the application’s nib file hasn’t finished loading yet or if the receiver is not active.
精彩评论