如何防止简单的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, 斯威夫特3
答案 0 :(得分:1)
您可以第二次出示提醒;如果您需要自定义超出该行为的行为,则需要避开NSAlert并运行自己制作的NSWindow或NSPanel。