I want to know how to open a new window on button click in Cocoa Mac Programming. Help me. I am doing a mac application which needs to open a new mac window on particular button click.
Asked
Active
Viewed 4.0k times
4 Answers
47
If you want to create a separate class for New Window, these are the steps:
- Create a class which is a sub class of NSWindowController e.g. NewWindowController
- Create a window xib for NewWindowController class.
On button click code as:
NewWindowController *windowController = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"]; [windowController showWindow:self];
-
thank you... it worked well... :) you explained well... thank you... I was looking for a few days and now it worked great... :) – ShinuShajahan Apr 06 '11 at 03:56
-
5I've tried the same thing, but the problem that I'm having is that the new window closes immediately after being displayed. I have no code in there whatsoever so I don;t see what I am doing wrong. Any ideas? – Interfector Apr 18 '12 at 22:34
-
@Interfector i am having the same problem .. did u figure it out ? – Gaurav_soni May 05 '12 at 08:58
-
Nope :| I'll figure it out sooner or later and I'll post the solution here. – Interfector May 14 '12 at 18:21
-
29@Interfector: The reason your window is immediately closing is possibly due to ARC. If you haven't assigned your window controller to a strongly held variable it will be freed immediately after the function ends. – iain Aug 29 '12 at 01:13
-
@iain: good point—as written above, the instance of `NewWindowController` in this answer still won't "stick" unless `controllerWindow` is, e.g., held in an instance variable outside the context of these two lines. – pje Sep 21 '12 at 08:15
-
Wanted to thank the poster of this answer... I was close (but still missed the mark) in my code using [[CustomWindowController alloc] init] instead of the -initWithWindowNibName:. This answer helped me! – Phil M May 25 '14 at 00:39
-
@iain: I have held my objects strong enough but still NSWindowController closes immediately. Please help. – Manthan Jun 10 '15 at 12:30
13
NSWindowController * wc=[[NSWindowController alloc] initWithWindowNibName:@"your_nib_name"];
[wc showWindow:self];

Saurabh
- 22,743
- 12
- 84
- 133
-
@Saurabh-wow! thanks a bunch! it's worked... thank you... I am new to stackoverflow and it's pretty much impressive... :) – ShinuShajahan Apr 06 '11 at 03:53
-
actually i was looking for the same for the past few days! :) I tried different but didn't subclass NSWindowController!!! – ShinuShajahan Apr 06 '11 at 04:10
-
Why was the sub-classing answer more highly rated than this one? Surely not subclassing is a simpler, more elegant answer? – fatuhoku Aug 09 '13 at 19:38
-
Getting Use of undeclared identifier 'self' here, using this inside main.m – Tejpal Rebari Apr 01 '21 at 12:01
11
Swift 3: In your storyboard go to WindowController -> Identity inspector -> storyBoardID: fill out: mainWindow. Then from your current viewcontroller link the button on the storyboard to the following method:
@IBAction func newWindow(_ sender: Any) {
let myWindowController = self.storyboard!.instantiateController(withIdentifier: "mainWindow") as! NSWindowController
myWindowController.showWindow(self)
}

Hans
- 1,886
- 24
- 18
7
- Create a class which is a sub class of NSWindowController e.g. NewWindowController
- Create a window xib for NewWindowController class.
On button click code as:
NewWindowController *controllerWindow = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"]; [controllerWindow showWindow:self];
Yes, but the window closes if this code is inside of some func. Here is solution.
In blah.h
@interface blah : NSObject {
...
NewWindowController *controllerWindow;
...
}
In blah.m
@implementation
...
-(IBAction)openNewWindow:(id)sender {
controllerWindow = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"];
[controllerWindow showWindow:self];
}
...

Devapploper
- 6,062
- 3
- 20
- 41

WildMassacre
- 103
- 2
- 6
-
2It's preferable to add a comment to the existing answer (when you are able) rather than quoting it, responding, and then posting your own. – Adrian Wragg Sep 03 '13 at 00:05
-
1
-
1Well... and how can I close the previous one? This code keeps two windows openned at once. – Marcelo Aug 05 '14 at 22:25
-
The problem with this, is that it doesn't emable to have multiple instances of the same window class open at once. For example say you have a text editor app and you want to view multiple text files at once. With this approach, as soon as you open the a new text file, the old window will instantly disappear, because you are overriding the current window controller instance with a new one. – Supertecnoboff Jun 27 '17 at 17:22