user486134
user486134

Reputation: 415

How to activate a window?

Though I searched and tried a lot, I can not make a second 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.

Upvotes: 0

Views: 1305

Answers (1)

Jim Correia
Jim Correia

Reputation: 7084

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.

Upvotes: 2

Related Questions