我想在我的 Swift 应用程序中打开一个新窗口,但我无法打开它。
class AppDelegate: NSObject, NSApplicationDelegate
{
func applicationDidFinishLaunching(aNotification: NSNotification)
{
openMyWindow()
}
func openMyWindow()
{
if let storyboard = NSStoryboard(name: "Main",bundle: nil)
{
if let vc = storyboard.instantiateControllerWithIdentifier("MyList") as? MyListViewController
{
var myWindow = NSWindow(contentViewController: vc)
myWindow.makeKeyAndOrderFront(self)
let controller = NSWindowController(window: myWindow)
controller.showWindow(self)
}
}
}
}
我已经在 IB 中设置了 Storyboard ID。
我已经跟踪了代码,它确实进入了窗口打开代码,但它什么也没做。
顺便说一句,我确实有一个默认的 Storyboard入口点设置,默认窗口打开正常,但我需要打开第二个窗口,这是不起作用的。
最佳答案
openMyWindow()
方法执行后,windowController 将被释放,因此窗口为nil。这就是它不存在的原因。
你必须在你的类中按住窗口以使其保持事件状态,然后窗 Eloquent 会可见。
var windowController : NSWindowController?
关于macos - 在 Swift 中打开新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30921737/