Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

macos

cocoa

xib

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.

like image 629
ShinuShajahan Avatar asked Apr 05 '11 05:04

ShinuShajahan


3 Answers

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];
    
like image 135
iPhoneDv Avatar answered Nov 11 '22 08:11

iPhoneDv


NSWindowController * wc=[[NSWindowController alloc] initWithWindowNibName:@"your_nib_name"];
[wc showWindow:self];
like image 22
Saurabh Avatar answered Nov 11 '22 08:11

Saurabh


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)
}
like image 37
Hans Avatar answered Nov 11 '22 08:11

Hans