在以下代码中,我正在检查UserDefaults。我想根据值做出反应。第一个问题是如何将两种情况合并为一个completionHandler?第二个问题是,在完成处理程序中,是否可以找到操作的按钮索引,而不是依赖标题来切换?预先感谢。
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")
}
}
答案 0 :(得分:2)
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")
}
}
仅创建案例中需要的内容。因此,设置一些变量(我称其为安装工作)来确定每种情况下将发生的变化。然后,在完成案例之后的所有工作之后,提出警报。
关于您的问题-每个按钮应具有不同的处理程序。它是(1)最易读的,而2)在耦合程度不高的地方提供的。允许多种用途。