0

如何防止简单的 NSAlert 被忽略?

F.ex.,当应用程序启动时,我显示一个包含 NSTextField 的 NSAlert。用户应输入密码。仅当密码正确时,才允许用户使用应用程序,如果不正确(如果密码不正确),则警报应留在那里并再次要求输入密码。

到目前为止,这是我的代码(用于创建警报):

func applicationDidFinishLaunching(_ aNotification: Notification){
    let alert = NSAlert()
    alert.addButton(withTitle: "Send")
    alert.delegate = self
    alert.alertStyle = .informational
    alert.messageText = "Password - Login"
    alert.informativeText = "Please type in your password: "

    let txt = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
    txt.stringValue = "Password:"


    alert.accessoryView = txt
    alert.beginSheetModal(for: NSApplication.shared().mainWindow!) { (response) in
        if (response == NSAlertFirstButtonReturn) {
            // the alert closes here, is there any way to prevent this?
        } else {
            print("No value.")
        }
    }

操作系统:OS X Sierra、Swift 3

4

1 回答 1

1

您可以再次显示警报;如果您需要自定义除此之外的行为,则需要避开 NSAlert 并运行您自己制作的 NSWindow 或 NSPanel。

于 2017-08-16T16:22:45.213 回答