35

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.

ShinuShajahan
  • 1,296
  • 2
  • 11
  • 20

4 Answers4

47

If you want to create a separate class for New Window, these are the steps:

  1. Create a class which is a sub class of NSWindowController e.g. NewWindowController
  2. Create a window xib for NewWindowController class.
  3. On button click code as:

    NewWindowController *windowController = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"];
    [windowController showWindow:self];
    
idmean
  • 14,540
  • 9
  • 54
  • 83
iPhoneDv
  • 1,969
  • 1
  • 19
  • 34
  • 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
  • 5
    I'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
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
  1. Create a class which is a sub class of NSWindowController e.g. NewWindowController
  2. Create a window xib for NewWindowController class.
  3. 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
  • 2
    It'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
    @AdrianWragg, yes, i am the newbie, so i'm sorry for that. – WildMassacre Sep 03 '13 at 18:38
  • 1
    Well... 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