NSAlert - 如何在 Swift3 中将视图呈现为 sheet 并获得 return 值?
NSAlert - how can I present a view as a sheet and get a return value in Swift3?
我有以下代码工作正常:
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
}
}
我正在努力执行相同的功能,但将警报显示为 sheet。
我不知道如何呈现和测试结果。我目前得到的全是真或全是假。
我已经尝试了各种排列,例如下面的排列,但并不愉快,如果有任何帮助,我将不胜感激。
//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
}
与 runModal
方法不同 beginSheetModal(for:completionHandler:)
异步工作。不可能 return 来自包含异步任务的函数/方法的值。
向 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
是可选的?它似乎是真正的 Bool
,只有 两个 状态。
我有以下代码工作正常:
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
}
}
我正在努力执行相同的功能,但将警报显示为 sheet。
我不知道如何呈现和测试结果。我目前得到的全是真或全是假。
我已经尝试了各种排列,例如下面的排列,但并不愉快,如果有任何帮助,我将不胜感激。
//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
}
与 runModal
方法不同 beginSheetModal(for:completionHandler:)
异步工作。不可能 return 来自包含异步任务的函数/方法的值。
向 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
是可选的?它似乎是真正的 Bool
,只有 两个 状态。