简体   繁体   English

如何在 Swift 4 Cocoa 应用程序的开头弹出两个窗口?

[英]How to make two windows pop out in the beginning on a Swift 4 Cocoa application?

I'm working on a Swift 4 Cocoa Mac OS X (not iOS) project with XCode 9. I have two NSWindow s in my main storyboard with different (subclasses of) NSView s.我正在使用 XCode 9 开发 Swift 4 Cocoa Mac OS X(不是 iOS)项目。我的主故事板中有两个NSWindow ,它们具有不同的(子类) NSView Currently only one window holds the storyboard entry point and so the other one does not appear when the application begins, and I want both windows to appear when the application is loaded.目前只有一个窗口包含情节提要入口点,因此在应用程序启动时不会出现另一个窗口,我希望在加载应用程序时出现两个窗口。

I've tried googiling with different keywords but so far couldn't find a way.我尝试使用不同的关键字进行谷歌搜索,但到目前为止找不到方法。 The only method I found was to connect a segue from a button or menu in one window to the other window, so that the other window appear whenever the button is pressed.我发现的唯一方法是将一个窗口中的按钮或菜单的 segue 连接到另一个窗口,以便在按下按钮时出现另一个窗口。 Is there any of making both windows appear in the beginning the 'right way' (preferablly using functionalities of XCode storyboard)?是否有任何方法使两个窗口都以“正确的方式”出现在开头(最好使用 XCode 故事板的功能)?

You have to get a reference to the window controller in your storyboard from the second window you want to show.您必须从要显示的第二个窗口中获取对情节提要中的窗口控制器的引用。 Add this code to your NSApplicationDelegate in applicationDidFinishLaunching .将此代码添加到applicationDidFinishLaunchingNSApplicationDelegate中。 Don't forget to set the identifier from the WindowController in the Storyboard不要忘记从 Storyboard 中的 WindowController 设置标识符

let storyboard = NSStoryboard(name: "Main", bundle: nil)
let windowController = storyboard.instantiateController(withIdentifier: "MyWindowController") as! NSWindowController
windowController.showWindow(self)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM