How to activate a window?
Though I searched and tried a lot, I can not make a secon开发者_如何学Cd window activated.
The code I used to display another window while main window is activating:
preferencesWindowController = (PreferencesWindowController*)[[NSWindowController alloc] initWithWindowNibName: @"Preferences"];
[preferencesWindowController showWindow: preferencesWindowController];
[[preferencesWindowController window] orderFrontRegardless];
[[preferencesWindowController window] makeKeyAndOrderFront: preferencesWindowController];
[NSApp activateIgnoringOtherApps:YES];
After trying debugger, I see that [preferencesWindowController window] is nil
preferencesWindowController = (PreferencesWindowController*)[[NSWindowController alloc] initWithWindowNibName: @"Preferences"];
[preferencesWindowController showWindow: self];
NSWindow* window = [preferencesWindowController window]; //---> nil
Why is it nil?
The nib file contains Window and PreferencesWindowController.
Sorry, I use multi nib wrong. I follow this example and have it works: http://maestric.com/doc/mac/cocoa/multiple_nibs . Instead of adding a window controller to nib, setting file's owner class to window controller.
This code is wrong:
preferencesWindowController = (PreferencesWindowController*)[[NSWindowController alloc] initWithWindowNibName: @"Preferences"];
Casting the result to PreferencesWindowController isn't going to change the type of the object which was created. You need to create the right kind of object:
preferencesWindowController = [[PreferencesWindowController alloc] initWithWindowNibName: @"Preferences"];
Also, ensure that you've actually connected the window outlet from File's Owner to the window in your nib.
精彩评论