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

前端 未结 4 790
轮回少年
轮回少年 2020-12-13 09:48

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 bu

相关标签:
4条回答
  • 2020-12-13 10:04

    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];
      
    0 讨论(0)
  • 2020-12-13 10: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)
    }
    
    0 讨论(0)
  • 2020-12-13 10:23
    NSWindowController * wc=[[NSWindowController alloc] initWithWindowNibName:@"your_nib_name"];
    [wc showWindow:self];
    
    0 讨论(0)
  • 2020-12-13 10:24
    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];
        }
    ...
    
    0 讨论(0)
提交回复
热议问题