How to get Main Window (App Delegate) from other class (subclass of NSViewController)?

后端 未结 3 1930
情书的邮戳
情书的邮戳 2020-12-23 22:01

I\'m trying to change my windows content, from other class , that is subclass of NSViewController.I\'m trying code below, but it doesn\'t do anything.

[NSApp         


        
3条回答
  •  一生所求
    2020-12-23 22:38

    Fighting with MacOS just figured this out.

    Apple's quote:

    mainWindow

    Property

    The app’s main window. (read-only)

    Discussion

    The value in this property is nil when the app’s storyboard or nib file has not yet finished loading. It might also be nil when the app is inactive or hidden.

    If you have only one window in your application (which is the most used case) use next code:

    NSWindow *mainWindow = [[[NSApplication sharedApplication] windows] objectAtIndex:0];
    

    Promise it won't be nil, if application has windows.

提交回复
热议问题