[英]Swift 4 UIAlertController how can I combine different actions into 1 completion handler?
In the following code I'm checking UserDefaults. 在以下代码中,我正在检查UserDefaults。 I want to react depending on the value. 我想根据值做出反应。 First question is how can I combine both cases into only 1 completionHandler? 第一个问题是如何将两种情况合并为一个completionHandler? And second question is, in the completion handler, can I find the button index of the action instead of my switch being dependent on the title? 第二个问题是,在完成处理程序中,是否可以找到操作的按钮索引,而不是依赖标题来切换? Thanks in advance. 提前致谢。
func checkUser() {
let registered = UserDefaults.standard.bool(forKey: "registered")
switch registered {
case true:
let firstName = UserDefaults.standard.string(forKey: "firstName") ?? ""
let lastName = UserDefaults.standard.string(forKey: "lastName") ?? ""
var fullName: String!
fullName = firstName
fullName.append(" \(lastName)")
let optionMenu = UIAlertController(title: "Please confirm", message: "Are you \n \(String(describing: fullName))", preferredStyle: .alert)
optionMenu.popoverPresentationController?.sourceView = self.view
optionMenu.popoverPresentationController?.sourceRect = self.view.bounds
let yesAction = UIAlertAction(title: "Yes", style:.destructive, handler: confirmHandler)
let noAction = UIAlertAction(title: "no", style: .destructive, handler: confirmHandler)
optionMenu.addAction(yesAction)
optionMenu.addAction(noAction)
let popover = optionMenu.popoverPresentationController
popover?.delegate = self
popover?.sourceView = view
popover?.sourceRect = CGRect(x: self.view.bounds.midX - 100, y: self.view.bounds.midY + 100, width: 200, height: 200)
DispatchQueue.main.async {
self.present(optionMenu, animated: true, completion: {})
}
case false:
let optionMenu = UIAlertController(title: "Please confirm", message: "Do you want to set up this iPad?", preferredStyle: .alert)
optionMenu.popoverPresentationController?.sourceView = self.view
optionMenu.popoverPresentationController?.sourceRect = self.view.bounds
let yesAction = UIAlertAction(title: "Yes", style: .default, handler: setUpHandler)
let noAction = UIAlertAction(title: "no", style: .default, handler: setUpHandler)
optionMenu.addAction(yesAction)
optionMenu.addAction(noAction)
let popover = optionMenu.popoverPresentationController
// popover?.delegate = self
popover?.sourceView = view
popover?.sourceRect = CGRect(x: self.view.bounds.midX - 150, y: self.view.bounds.midY, width: 0, height: 0)
DispatchQueue.main.async {
self.present(optionMenu, animated: true, completion: {})
}
}
}
func setUpHandler (alert: UIAlertAction) {
print ("received: \(String(describing: alert.title))")
switch alert.title {
case "Yes":
print("show set up")
case "No":
print("show set up")
default:
print("show set up")
}
}
func checkUser() {
let registered = UserDefaults.standard.bool(forKey: "registered")
let title = "Please Confirm"
var message:String?
var actions:[UIAlertAction]! = []
var rect:CGRect!
switch registered {
case true:
let firstName = UserDefaults.standard.string(forKey: "firstName") ?? ""
let lastName = UserDefaults.standard.string(forKey: "lastName") ?? ""
var fullName: String!
fullName = firstName
fullName.append(" \(lastName)")
let yesAction = UIAlertAction(title: "Yes", style:.destructive, handler: confirmHandler)
let noAction = UIAlertAction(title: "no", style: .destructive, handler: confirmHandler)
actions.append(yesAction)
actions.append(noAction)
rect = CGRect(x: self.view.bounds.midX - 100, y: self.view.bounds.midY + 100, width: 200, height: 200)
break
case false:
let yesAction = UIAlertAction(title: "Yes", style: .default, handler: setUpHandler)
let noAction = UIAlertAction(title: "no", style: .default, handler: setUpHandler)
actions.append(yesAction)
actions.append(noAction)
rect = CGRect(x: self.view.bounds.midX - 150, y: self.view.bounds.midY, width: 0, height: 0)
break
}
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.popoverPresentationController?.sourceView = self.view
alert.popoverPresentationController?.sourceRect = self.view.bounds
for action in actions {
alert.addAction(action)
}
let popover = alert.popoverPresentationController
popover?.delegate = self
popover?.sourceView = view
DispatchQueue.main.async {
self.present(alert, animated: true, completion: nil)
}
}
func setUpHandler (alert: UIAlertAction) {
print ("received: \(String(describing: alert.title))")
switch alert.title {
case "Yes":
print("show set up")
case "No":
print("show set up")
default:
print("show set up")
}
}
Create only what you NEED inside the cases. 仅创建案例中需要的内容。 So, set some variables (what I call setup work) to establish what will CHANGE per case. 因此,设置一些变量(我称其为安装工作)来确定每种情况下将发生的变化。 Then, after ALL the work is done after the cases, present the alert. 然后,在完成案例之后的所有工作之后,提出警报。
In regards to your questions -- you should have each button with a different handler. 关于您的问题-每个按钮应具有不同的处理程序。 It's 1) most readable and 2) provides it where it is not so coupled. 它是(1)最易读的,而2)在耦合程度不高的地方提供的。 Allows for multiple uses. 允许多种用途。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.