我试图在我的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.
执行openMyWindow()方法后,windowController将被释放,因此窗口为零.这就是它不存在的原因.
您必须在您的班级中保持窗口以使其保持活动状态,然后窗口将可见.
var windowController : NSWindowController?