如何在OS X故事板的开头隐藏初始窗口

Mat*_*oli 12 macos cocoa uistoryboard swift

我正在创建一个OS X状态栏应用程序,所以我希望应用程序开始隐藏.

我创建了一个"故事板"应用程序,初始窗口始终显示,即使"未启动时可见"(默认情况下未选中)也是如此.


注意:如果我禁用" 是初始控制器 ",那么应用程序正确启动时没有任何窗口,但我的(现在的孤儿)窗口似乎永远不会添加到故事板:

var mainWindow = NSStoryboard(name: "Main", bundle: nil)?.instantiateControllerWithIdentifier("mainWindow")
Run Code Online (Sandbox Code Playgroud)

找不到"mainWindow"控制器(即使我在Window Controller上正确设置了"Storyboard ID").

所以我认为离开" 是初始控制器 " 更好,但只是在开始时隐藏主窗口......

Tim*_*del 30

取消选中故事板上的"Is Initial Controller"框,让您的应用程序没有初始控制器.您的应用程序将运行,但没有窗口.

截图


kmi*_*ael 11

这可能有点像黑客,但你可以做到这一点

func applicationDidFinishLaunching(notification: NSNotification) {
    // Insert code here to initialize your application
    NSApplication.sharedApplication().windows.last!.close()
}
Run Code Online (Sandbox Code Playgroud)

然后......

NSApplication.sharedApplication().windows.last!.makeKeyAndOrderFront(nil)
NSApplication.sharedApplication().activateIgnoringOtherApps(true)
Run Code Online (Sandbox Code Playgroud)


小智 6

取消选中"是初始控制器",但是您需要NSWindowController手动设置故事板及其关联.

这个答案的确切方法显示在这个答案中,我将在这里引用:

[...]在你的AppDelegate,为窗口控制器设置一个属性:

@property NSWindowController *myController;
Run Code Online (Sandbox Code Playgroud)

applicationDidFinishLaunching:方法实现中,创建对Storyboard的引用.这样您就可以从故事板访问窗口控制器.之后,剩下要做的就是通过向窗口控制器发送showWindow:方法来显示窗口.

#import "AppDelegate.h"

@interface AppDelegate ()
@end

@implementation AppDelegate

@synthesize myController;

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

@end
Run Code Online (Sandbox Code Playgroud)