ios - 如何在swift 5中使用闭包在参数中传递函数
问题描述
我是新来的。我制作了通用操作表
import Foundation
extension UIAlertController{
func action(mes:String,tit:String,tit2:String,operation1:(),operation2:()) {
let actionSheet = UIAlertController(title: "", message:mes, preferredStyle: .actionSheet)
let EButton = UIAlertAction(title:tit ,
style: .default,
handler: { _ in
operation1
})
let AllEButton = UIAlertAction(title:tit2,
style: .default ,
handler:{ _ in
operation2
})
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel,
handler: nil)
[EButton, AllEButton, cancelAction].forEach { $0.setValue(UIColor.red, forKey: "titleTextColor")
}
actionSheet.addAction(EButton)
actionSheet.addAction(AllEButton)
actionSheet.addAction(cancelAction)
present(actionSheet, animated: true, completion: nil)
}
}
我想从 viewControllerA 调用这个扩展
let actionView = UIAlertController()
class viewControllerA: UIViewController {}
private extension viewControllerA {
func alertBottomSheat() {
actionView.action(mes: "Update",tit: "Update only",tit2: "Update All", operation1:saveEvent(),operation2:saveEvent())
}
@IBAction func deleteEventButtonClicked(_ sender: Any) {
actionView.action(mes: "delete ",tit: "Delete only",tit2: "Delete All ",operation1:deleteEvent(),operation2:deleteEvent(deleteAll: true))
}
}
Q1-我是从 viewControllerA 扩展中调用扩展的正确方法吗?
Q2-请告诉我如何在这一行使用闭包在动作函数参数中传递函数?
actionView.action(mes: "delete ",tit: "Delete only",tit2: "Delete All ",operation1:deleteEvent(),operation2:deleteEvent(deleteAll: true))
以及如何在这一行的操作表处理程序中使用闭包
let EButton = UIAlertAction(title:tit ,
style: .default,
handler: { _ in
operation1
})
解决方案
您首先需要创建一个扩展UIViewController
而不是UIAlertController
另外,为函数设置正确的闭包参数,然后像这样调用函数。
extension UIViewController {
func action(message: String, firstTitle: String, secondTitle: String, firstAction: (() -> Void)? = nil, secondAction: (() -> Void)? = nil) {
let actionSheet = UIAlertController(title: "", message: message, preferredStyle: .actionSheet)
let eButton = UIAlertAction(title: firstTitle ,
style: .default,
handler: {_ in firstAction?()})
let allEButton = UIAlertAction(title: secondTitle,
style: .default ,
handler: {_ in secondAction?()})
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel,
handler: nil)
[eButton, allEButton, cancelAction].forEach { $0.setValue(UIColor.red, forKey: "titleTextColor")}
actionSheet.addAction(eButton)
actionSheet.addAction(allEButton)
actionSheet.addAction(cancelAction)
present(actionSheet, animated: true, completion: nil)
}
}
用法
private extension viewControllerA {
func alertBottomSheat() {
self.action(message: "Update", firstTitle: "Update only", secondTitle: "Update All", firstAction: saveEvent, secondAction: saveEvent)
}
@IBAction func deleteEventButtonClicked(_ sender: Any) {
self.action(message: "delete ", firstTitle: "Delete only", secondTitle: "Delete All ", firstAction: { self.deleteEvent()}, secondAction: { self.deleteEvent(deleteAll: true) })
}
func saveEvent() {
}
func deleteEvent(deleteAll: Bool = false) {
}
}
注意:固定编码标准规则和变量名称。
推荐阅读
- python - pip install s3fs with sagemaker-training inside docker 导致兼容性问题
- powershell - Powershell 转换时间
- typescript - Typescript - 指定“key”类型,其中 Object[key of Object] 必须返回 string[]
- php - ErrorException 未定义的偏移量:1
- c - 如何在 C/Linux 中的计时器到期后执行代码后阻塞操作?
- c# - 充气城堡添加额外的填充块
- css - 进度条颜色完全没有变化
- c# - 为什么我下载的 blob 损坏了?
- django - Angular 通用 SSR 在从 Django 提供服务时不显示 Angular 资产文件夹图像
- java - 尝试调试远程 Wildfly 20 服务器失败