【问题标题】:How to prevent NSAlert from dismiss如何防止 NSAlert 被解雇
【发布时间】:2017-08-16 15:00:46
【问题描述】:

如何防止简单的 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

【问题讨论】:

    标签: swift nsalert


    【解决方案1】:

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

    【讨论】:

    • 谢谢,我会尝试使用 NSPanel 制作自己的“警报”。顺便说一句,你知道为什么我的问题被否决了吗?是不是太短了,我应该补充一下吗?
    • 我不是反对它的人之一,但问题很简短。一般来说,最好更清楚并提供更多关于您正在尝试做什么的详细信息。
    • 好的,谢谢。我真的不是有意惹恼某人。我将对其进行编辑并尝试使其更精确。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    相关资源
    最近更新 更多