Jesus
Jesus

Reputation: 8576

How to open an NSWindowController from another NSWindowController in Cocoa

I'm developing an app in cocoa for MacOSX in Xcode5 and I want to open another window from my current window by pressing a button, this is my code:

- (IBAction)openWindow:(id)sender {

    WindowController *controllerWindow = [[WindowController alloc] initWithWindowNibName:@"WindowController"];
    [controllerWindow showWindow:nil];
    [[controllerWindow window] makeMainWindow];
    }

so far I can see the window appearing by one second and then this just dissappear, how to do this correctly???

Upvotes: 3

Views: 1235

Answers (2)

uchuugaka
uchuugaka

Reputation: 12782

Neither the window nor the window controller have a strong reference anywhere outside the scope of this method. So after that, they're getting released.

Normally, you would add your window controller to some container like an array in your app delegate. The array will retain the window controller. The window controller can hang on to the window.

It also makes sense for the action method to be in the app delegate. The button should just send a selector up the responder chain (the loose coupling of the responder chain is lovely).

Upvotes: 6

user6502515
user6502515

Reputation: 253

use this..

Create a new .h & .m files which yo need to open, as NewWindowController (for eg.) along with its .xib

And on any button click, to open the newly defined window, just allocate, instantiate and present..

NewWindowController *controllerWindow = [[NewWindowController alloc] initWithWindowNibName:@"NewWindowController"];
[controllerWindow showWindow:self];

Upvotes: 0

Related Questions