如何在swift 5中使用闭包在参数中传递函数

获取

我是 swift 的新手。我已经制作了通用操作表

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) {
        
    }
}

注意:固定编码标准规则和变量名称。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Powershell CLI中传递函数参数?

在Javascript中传递函数时如何处理参数?

在汇编中调用符号时如何传递函数参数?

下面的promise代码中是如何传递函数参数的

如何在关键字参数中使用@retry并传递函数

如何在R中的SQL查询中传递函数参数?

如何在solidity中使用传递函数

在Swift 3中传递函数

如何在Processing中传递函数作为类的输入

如何在 julia 中传递函数类型安全

Vue,如何在JSX渲染的props中传递函数?

如何在python中传递函数的值?

如何在C中传递函数多维char数组?

如何在elisp中递归地传递函数?

如何在oop中制作传递函数

如何访问已在闭包中使用use()传递的ReflectionFunction中的参数

如何在Swift 3中记录函数的闭包参数的参数?

如何在Swift中设置对闭包/函数的弱引用?

Swift-使用闭包在init方法中设置属性

闭包如何在作为参数传递的函数表达式中起作用?

如何在Swift 3中使用关联类型的参数调用泛型函数

如何在Swift 4中使用函数currying

如何在Swift 3中使用glVertexAttribPointer函数?

如何在Swift 3.0中使用函数

如何从具有串联html + object值的变量中传递函数参数?

在我的ViewController中,如何传递函数作为参数并保持[弱自我]?

如何使用骨干JS动态传递函数的diff参数?

使用timeit.Timer()时如何传递函数的参数

如何在IN子句中使用的函数中传递参数数量