Reputation: 1296
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
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
Reputation: 1969
If you want to create a separate class for New Window, these are the steps:
On button click code as:
NewWindowController *windowController = [[NewWindowController alloc] initWithWindowNibName:@"You Window XIB Name"];
[windowController showWindow:self];
Upvotes: 47
Reputation: 113
- 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];
}
...
Upvotes: 7
Reputation: 22873
NSWindowController * wc=[[NSWindowController alloc] initWithWindowNibName:@"your_nib_name"];
[wc showWindow:self];
Upvotes: 13