我如何防止一个简单的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: OS,Swift 3
发布于 2017-08-16 16:22:45
您可以第二次显示警报;如果需要自定义该行为之外的行为,则需要避免使用NSAlert,并运行自己创建的NSWindow或NSPanel。
https://stackoverflow.com/questions/45717051
复制相似问题