ShinuShajahan
ShinuShajahan

Reputation: 1296

How to open a new window on button click in Cocoa Mac Application?

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.

Upvotes: 35

Views: 40152

Answers (4)

Hans
Hans

Reputation: 1915

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)
}

Upvotes: 11

iPhoneDv
iPhoneDv

Reputation: 1969

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];
    

Upvotes: 47

WildMassacre
WildMassacre

Reputation: 113

  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];
    }
...

Upvotes: 7

Saurabh
Saurabh

Reputation: 22873

NSWindowController * wc=[[NSWindowController alloc] initWithWindowNibName:@"your_nib_name"];
[wc showWindow:self];

Upvotes: 13

Related Questions