Macos 如何隐藏“从OSX开始”故事板上的初始窗口

Macos 如何隐藏“从OSX开始”故事板上的初始窗口,macos,cocoa,swift,uistoryboard,Macos,Cocoa,Swift,Uistoryboard,我正在创建一个OSX状态栏应用程序,因此我希望该应用程序开始隐藏 我创建了一个“故事板”应用程序,初始窗口始终显示,即使“启动时可见”未选中(默认情况下未选中) 注意:如果禁用“Is initial controller”,则应用程序将在没有任何窗口的情况下正确启动,但我的(现在是孤立的)窗口似乎永远不会添加到情节提要: var mainWindow = NSStoryboard(name: "Main", bundle: nil)?.instantiateControllerWithIden

我正在创建一个OSX状态栏应用程序,因此我希望该应用程序开始隐藏

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


注意:如果禁用“Is initial controller”,则应用程序将在没有任何窗口的情况下正确启动,但我的(现在是孤立的)窗口似乎永远不会添加到情节提要:

var mainWindow = NSStoryboard(name: "Main", bundle: nil)?.instantiateControllerWithIdentifier("mainWindow")
找不到“主窗口”控制器(即使我在窗口控制器上正确设置了“情节提要ID”)


因此,我认为最好离开“Is initial controller”,但只是在开始时隐藏主窗口…

这可能有点麻烦,但您可以做到这一点

func applicationDidFinishLaunching(notification: NSNotification) {
    // Insert code here to initialize your application
    NSApplication.sharedApplication().windows.last!.close()
}
后来

NSApplication.sharedApplication().windows.last!.makeKeyAndOrderFront(nil)
NSApplication.sharedApplication().activateIgnoringOtherApps(true)
取消选中故事板上的“初始控制器”框,使您的应用程序没有初始控制器。你的应用程序将运行,但没有窗口

取消选中“Is Initial Controller”,然后需要手动设置情节提要及其关联的
NSWindowController

具体做法如下所示,我将在此处引用:

[…]在
AppDelegate
中,为窗口控制器设置属性:

@property NSWindowController *myController;
ApplicationIDFinishLaunching:
方法实现中,创建对情节提要的引用。通过这种方式,您可以从情节提要访问窗口控制器。在此之后,唯一要做的事情就是通过向窗口控制器发送
showWindow:
方法来显示窗口


这样做的方法就像您尝试过的:

let storyboard = NSStoryboard(name: "Main", bundle: nil)
guard let mainWC = storyboard.instantiateControllerWithIdentifier("MainWindowController") as? MainWindowController else {
   fatalError("Error getting main window controller")
}
// optionally store the reference here
self.mainWindowController = mainWC

mainWC.window?.makeKeyAndOrderFront(nil) // or use `.showWindow(self)`
您唯一可能忘记的是取消选中“关闭时释放”

这将立即释放窗口,并阻止脚本加载机制找到它,即使您有正确的标识符。

在这种情况下如何显示窗口?请参阅使用此解决方案而不是上面的hack,感谢Tim原谅我在一年多前回答了这个问题,但是即使您没有将窗口设置为初始视图控制器,也可以从故事板实例化窗口。我想,也许你没有持有窗口或窗口控制器实例。这对我来说很有效,但我必须在操作块中显示窗口,因为它根本没有显示。OperationQueue.main.addOperation{window?.MakeKeyandDerfront(nil)NSApplication.shared().activate(ignoringOtherApps:true)}为什么苹果让事情变得困难。这是一个用XIB勾选一个方框的问题。谢谢苹果。
let storyboard = NSStoryboard(name: "Main", bundle: nil)
guard let mainWC = storyboard.instantiateControllerWithIdentifier("MainWindowController") as? MainWindowController else {
   fatalError("Error getting main window controller")
}
// optionally store the reference here
self.mainWindowController = mainWC

mainWC.window?.makeKeyAndOrderFront(nil) // or use `.showWindow(self)`