las*_*302 0 ios swift uialertcontroller
我正在创建一个返回布尔值的函数。但是 Swift 向我展示了错误:
预期返回“Bool”的函数中缺少返回值
我的函数不直接返回“布尔”,它有一些条件,比如如果用户点击“确定”按钮,那么它应该返回真,如果“取消”则返回假。有人可以告诉我如何解决这个问题吗?请参阅以下代码以供参考:
func showConfirmationAlert(title: String!, message: String!) -> Bool {
dispatch_async(dispatch_get_main_queue(), {
let alertController=UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let okButton=UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default) { (okSelected) -> Void in
return true
}
let cancelButton=UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (cancelSelected) -> Void in
return false
}
alertController.addAction(okButton)
alertController.addAction(cancelButton)
if self.presentedViewController == nil {
self.presentViewController(alertController, animated: true, completion: nil)
}
})
}
Run Code Online (Sandbox Code Playgroud)
认为你想要这样的东西
func showConfirmationAlert(title: String!, message: String!,success: (() -> Void)? , cancel: (() -> Void)?) {
dispatch_async(dispatch_get_main_queue(), {
let alertController = UIAlertController(title:title,
message: message,
preferredStyle: UIAlertControllerStyle.Alert)
let cancelLocalized = NSLocalizedString("cancelButton", tableName: "activity", comment:"")
let okLocalized = NSLocalizedString("viewDetails.button", tableName: "Localizable", comment:"")
let cancelAction: UIAlertAction = UIAlertAction(title: cancelLocalized,
style: .Cancel) {
action -> Void in cancel?()
}
let successAction: UIAlertAction = UIAlertAction(title: okLocalized,
style: .Default) {
action -> Void in success?()
}
alertController.addAction(cancelAction)
alertController.addAction(successAction)
self.presentViewController(alertController, animated: true, completion: nil)
})
}
showConfirmationAlert("mytitle", message: "body", success: { () -> Void in
print("success")
}) { () -> Void in
print("user canceled")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2454 次 |
| 最近记录: |