gpt4 book ai didi

ios - 如果需要处理函数,则无法将 alertController 移动到 UIViewController 扩展

转载 作者:搜寻专家 更新时间:2023-11-01 06:11:34 31 4
gpt4 key购买 nike

在我的程序中,我多次调用 UIAlertControllers。为了让事情更容易阅读,如果 AlertController 只有一个“OK” Action 且处理程序为 nil,我就能够成功地排除它。

如果我想传递一个我想链接为 UIAlertAction 的函数,我很难做同样的事情。 func showOKCancelAlertController 不编译

showOKAlertController(title: "Network Error", message: "Unable to download photos") //This is an example of how I call Alert Controller throughout my code.

extension UIViewController {
func showOKAlertController(title: String, message: String){
let myAlertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
myAlertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(myAlertController, animated: true)
}

func showOKCancelAlertController(title: String, message: String, okFunction: UIAlertAction ){
let myAlertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
myAlertController.addAction(UIAlertAction(title: "OK", style: .default, handler: okFunction))
myAlertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
present(myAlertController, animated: true)
}
}

最佳答案

显然,您面临的错误是:

Cannot convert value of type 'UIAlertAction' to expected argument type '((UIAlertAction) -> Void)?'

那是因为 okFunction 参数类型为 UIAlertAction,这是不正确的部分。您应该让 okFunction 成为 ((UIAlertAction) -> Void)? 类型:

func showOKCancelAlertController(title: String, message: String, okFunction: ((UIAlertAction) -> Void)?) {
let myAlertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
myAlertController.addAction(UIAlertAction(title: "OK", style: .default, handler: okFunction))
myAlertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
present(myAlertController, animated: true)
}

那是因为 UIAlertAction 初始化签名是:

init(title: String?, style: UIAlertAction.Style, handler: ((UIAlertAction) -> Void)? = nil),

handler 参数需要 ((UIAlertAction) -> Void)?

因此,您称它为:

showOKCancelAlertController(title: "title", message: "message") { _ in
print("here is what to do when tapping the OK button")
}

此外,如果 OK 按钮没有 Action ,您可以为 okFunction 参数提供默认的 nil 值:

func showOKCancelAlertController(title: String, message: String, okFunction: ((UIAlertAction) -> Void)? = nil)

并将其称为:

showOKCancelAlertController(title: "title", message: "message")

实际上,这对您的案例来说是一件很酷的事情:此时您甚至不需要实现两种不同的方法,您可以只实现一种方法并传递 okFunction仅在需要时为其设置参数!示例:

func showAlertController(title: String, message: String, okFunction: ((UIAlertAction) -> Void)? = nil) {
let myAlertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
myAlertController.addAction(UIAlertAction(title: "OK", style: .default, handler: okFunction))
if let okFun = okFunction {
myAlertController.addAction(UIAlertAction(title: "Cancel", style: .default, handler: okFun))
}

present(myAlertController, animated: true)
}

如果要一键显示:

showAlertController(title: "title", message: "message")

对于两个按钮:

showAlertController(title: "title", message: "message") { _ in
// ...
}

关于ios - 如果需要处理函数,则无法将 alertController 移动到 UIViewController 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55676426/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com