Initializing another window using storyboard for OS X

前端 未结 4 1683
春和景丽
春和景丽 2021-02-02 09:46

I have created a Cocoa application in Xcode6 which uses storyboards. As a template, Xcode provides a window for the application. I want to add a second window to show when the p

4条回答
  •  無奈伤痛
    2021-02-02 10:16

    In your Storyboard, select your second Window Controller. In the identity inspector, specify a name for this window controller, e.g secondWindowController

    Then, in your app delegate, set up a property for the window controller:

    @property NSWindowController *myController;
    

    In your applicationDidFinishLaunching: method implementation, create a reference to the Storyboard. This way you get access your window controller from the storyboard. After that, the only thing left to do is to display the window by sending your window controller the showWindow: method.

    #import "AppDelegate.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    @synthesize myController;
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSStoryboard *storyBoard = [NSStoryboard storyboardWithName:@"Main" bundle:nil]; // get a reference to the storyboard
    myController = [storyBoard instantiateControllerWithIdentifier:@"secondWindowController"]; // instantiate your window controller
    [myController showWindow:self]; // show the window
    }
    
    @end
    

提交回复
热议问题