NSAlert - 如何将视图作为工作表呈现并在Swift3中获取返回值?

时间:2017-03-04 17:47:01

标签: cocoa swift3 macos-sierra

我有以下代码正常工作:

 func alertDialog(question: String, text: String) -> Bool {
    let alert: NSAlert = NSAlert()
    alert.messageText = question
    alert.informativeText = text
    alert.alertStyle = NSAlertStyle.warning
    alert.addButton(withTitle: "Yes Please")
    alert.addButton(withTitle: "No Thank You")
     changedItem = false
  return alert.runModal() == NSAlertFirstButtonReturn

我像这样检查NSAlertFirstButtonReturn的值:

 func tableViewSelectionDidChange(_ notification: Notification)
    {
        print (#function, "changedItem", changedItem!)
        if changedItem == true {
            let answer = alertDialog(question: "Save your changes?", text: "Unsaved changes will be shredded.")
            print(#function, "answer: ", answer)
            if answer == true {
                print(#function, "TRUE")
                saveChanges(self)
                changedItem = false
            }
        }

我正在努力执行相同的功能,但将警报显示为表格。

我无法想象如何展示和测试结果。我现在得到的都是真的或全是假的。

我尝试了各种各样的排列,如下所示,没有任何快乐,并且会感激任何帮助。

//instead of the return sheetModal

        var result: Int! = 0
        //result = false
        alert.beginSheetModal(for: self.view.window!, completionHandler: { (NSAlertFirstButtonReturn) -> Void in
            if NSAlertFirstButtonReturn.hashValue  == 1000 {
                result = 1
            } else {
                result = 0
            }


        })
         print(#function, "NSAlertFirstButtonReturn: ", NSAlertFirstButtonReturn.hashValue)
        changedItem = false

        if result == 1 {
            return true
        } else {
            return false
        }

1 个答案:

答案 0 :(得分:1)

RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$ RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 方法runModal异步工作不同。无法从包含异步任务的函数/方法返回值。

beginSheetModal(for:completionHandler:)方法添加完成处理程序:

alertDialog

并使用方法:

func alertDialog(question: String, text: String, completion: @escaping (Bool)->() ) {
    let alert: NSAlert = NSAlert()
    alert.messageText = question
    alert.informativeText = text
    alert.alertStyle = NSAlertStyle.warning
    alert.addButton(withTitle: "Yes Please")
    alert.addButton(withTitle: "No Thank You")
    changedItem = false
    alert.beginSheetModal(for: self.view.window!, completionHandler: { result in
        completion(result == NSAlertFirstButtonReturn)
    })
}

功能实际上是相同的,但执行是异步的。

旁注:为什么func tableViewSelectionDidChange(_ notification: Notification) { print (#function, "changedItem", changedItem!) if changedItem == true { alertDialog(question: "Save your changes?", text: "Unsaved changes will be shredded.", completion: { [unowned self] answer in print(#function, "answer: ", answer) if answer == true { print(#function, "TRUE") self.saveChanges(self) self.changedItem = false } }) } } 是可选的?它似乎是一个真正的changedItem只有两个状态。