作者:沈达浪认_972 | 来源:互联网 | 2023-12-13 13:47
有没有一种方法可以在不继承UIAlertController的子类或不涉及UIAlertactions的情况下执行代码块?
我需要在被解雇时安全处置一个与警报文本文件绑定的可观察对象。
我认为您必须使用警报操作,但是如果需要,您可以将其与完成功能混在一起,
func showAlertMessageCompletion(titleStr:String,messageStr:String,completion: @escaping ((Bool) -> Void)) {
let alert = UIAlertController(title: titleStr,message: messageStr,preferredStyle: UIAlertController.Style.alert)
let action = UIAlertAction(title: "Aceptar",style: .default) { (_) in
completion(true)
}
alert.addAction(action)
self.present(alert,animated: true,completion: nil)
}
,
您需要将处理程序添加到警报操作按钮。
let alertCOntroller= UIAlertController(title: "Alert title",message: "Message to display",preferredStyle: .alert)
// Create OK button
let OKAction = UIAlertAction(title: "OK",style: .default) { (action:UIAlertAction!) in
// Code in this block will trigger when OK button tapped.
print("Ok button tapped");
}
alertController.addAction(OKAction)
// Create Cancel button
let cancelAction = UIAlertAction(title: "Cancel",style: .cancel) { (action:UIAlertAction!) in
print("Cancel button tapped");
}
alertController.addAction(cancelAction)
// Present Dialog message
self.present(alertController,completion:nil)