作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的代码中,我正在检查 UserDefaults。我想根据值(value)使用react。第一个问题是如何将这两种情况组合成一个 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")
}
}
最佳答案
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) 在不那么耦合的地方提供它。允许多次使用。
关于ios - Swift 4 UIAlertController 如何将不同的操作组合到 1 个完成处理程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53343479/
我是一名优秀的程序员,十分优秀!