简体   繁体   中英

Swift mac app -> Moving cursor closes NSWindow

I am new MAC application development using swift. I have a NSViewController with different menu options.

In which, I have one NSWindow open already and tapping "Window" menu option, opening another window.

let passwordVC = PasswordViewController()
        let pwdWindow = NSWindow(contentViewController: passwordVC)
        pwdWindow.title = "Password"
        pwdWindow.orderFront(self)
        let controller = NSWindowController(window: pwdWindow)

        controller.showWindow(self)

but it closes automatically when I start moving cursor. I am doing nothing, it stays. I am not sure what is the issue. Kindly help.

From the posted code, your window is going out of scope, which will destroy it. Try retaining the window in a higher scoped variable. Like as a class property:

class Controller: NSViewController {
    var pwdWindow: NSWindow!
    override func viewDidLoad() {
        pwdWindow = NSWindow(contentViewController: passwordVC)
    }
}

I am using like this. And it works for me. Seems to be an alternate way.

 let passwordVC = PasswordViewController()
        let pwdWindow = NSWindow(contentViewController: passwordVC)
        pwdWindow.title = "Password"
        let application = NSApplication.shared()
        application.runModal(for: pwdWindow)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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