Swift 5 uialertController 中的完成处理程序哪个更好用?
Swift 5 which is the better usage of completition handler in uialertController?
关于警报的关闭和竞争 Hanlder 的一些说明。这是更好的用法?如果我应该如何在第二种情况下使用 "action" ?结果似乎是一样的,它有效,但我想更好地理解为什么。
import UIKit
struct exapleStruct {
var inHotel = true
}
class ViewController : UIViewController {
var exapleStruct : exapleStruct!
var detailTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let myAlertController = UIAlertController(title: "Add or Change value", message: "", preferredStyle: .alert)
//firstExample
let booleanChange = UIAlertAction(title: "change", style: .default, handler: self.handlerForBool)
//second exampple how shoukld I use "action" ?? why is it there?
let booleanChange2 = UIAlertAction(title: "change", style: .default) { (action) in
print(self.exapleStruct.inHotel)
self.detailTable.reloadData()
}
myAlertController.addAction(booleanChange)
present(myAlertController, animated: true, completion: nil)
}
func handlerForBool(alertARgument: UIAlertAction!) {
print(exapleStruct.inHotel)
self.detailTable.reloadData()
}
}
使用这个
let booleanChange2 = UIAlertAction.init(title: "option1", style: .default,
handler: handlerForBool(alertARgument:))
let booleanChange3 = UIAlertAction.init(title: "option2", style: .default,
handler: handlerForBool(alertARgument:))
func handlerForBool(alertARgument: UIAlertAction!) {
print(exapleStruct.inHotel)
self.detailTable.reloadData()
}
当您需要对多个 alertActions 执行相同的操作时很有用,它是函数的可重用性
关于警报的关闭和竞争 Hanlder 的一些说明。这是更好的用法?如果我应该如何在第二种情况下使用 "action" ?结果似乎是一样的,它有效,但我想更好地理解为什么。
import UIKit
struct exapleStruct {
var inHotel = true
}
class ViewController : UIViewController {
var exapleStruct : exapleStruct!
var detailTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let myAlertController = UIAlertController(title: "Add or Change value", message: "", preferredStyle: .alert)
//firstExample
let booleanChange = UIAlertAction(title: "change", style: .default, handler: self.handlerForBool)
//second exampple how shoukld I use "action" ?? why is it there?
let booleanChange2 = UIAlertAction(title: "change", style: .default) { (action) in
print(self.exapleStruct.inHotel)
self.detailTable.reloadData()
}
myAlertController.addAction(booleanChange)
present(myAlertController, animated: true, completion: nil)
}
func handlerForBool(alertARgument: UIAlertAction!) {
print(exapleStruct.inHotel)
self.detailTable.reloadData()
}
}
使用这个
let booleanChange2 = UIAlertAction.init(title: "option1", style: .default,
handler: handlerForBool(alertARgument:))
let booleanChange3 = UIAlertAction.init(title: "option2", style: .default,
handler: handlerForBool(alertARgument:))
func handlerForBool(alertARgument: UIAlertAction!) {
print(exapleStruct.inHotel)
self.detailTable.reloadData()
}
当您需要对多个 alertActions 执行相同的操作时很有用,它是函数的可重用性