将闭包作为目标添加到 UIButton [英] Adding a closure as target to a UIButton

查看:20
本文介绍了将闭包作为目标添加到 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用控件类,它需要根据视图控制器设置按钮的完成.由于 setLeftButtonActionWithClosure 函数需要将闭包作为参数,该闭包应该设置为取消按钮的操作.它会如何在 Swift 中是可能的,因为我们需要将函数名作为字符串传递给 action: 参数.

func setLeftButtonActionWithClosure(completion: () -> Void){self.leftButton.addTarget(<#target: AnyObject?#>, 动作: <#Selector#>, forControlEvents: <#UIControlEvents#>)}

解决方案

不要使用这个答案,请看下面的注释

注意:就像@EthanHuang 说的"如果您有两个以上的实例,此解决方案将不起作用.所有操作都将被最后一个分配覆盖."开发时请记住这一点,我会尽快发布另一个解决方案.

如果要将闭包作为目标添加到 UIButton,则必须使用 extensionUIButton 类p>

斯威夫特 5

导入 UIKit扩展 UIButton {私人 func actionHandler(action:(() -> Void)? = nil) {struct __ { static var action :(() -> Void)?}如果动作 != nil { __.action = 动作 }否则 { __.action?() }}@objc 私有函数 triggerActionHandler() {self.actionHandler()}func actionHandler(controlEvents control :UIControl.Event, ForAction action:@escaping () -> Void) {self.actionHandler(动作:动作)self.addTarget(self, action: #selector(triggerActionHandler), for: control)}}

老年人

导入 UIKit扩展 UIButton {私人 func actionHandleBlock(action:(() -> Void)? = nil) {结构__{静态变量操作:(()->无效)?}如果行动!=无{__.action = 动作} 别的 {__.行动?()}}@objc 私有函数 triggerActionHandleBlock() {self.actionHandleBlock()}func actionHandle(controlEvents control :UIControlEvents, ForAction action:() -> Void) {self.actionHandleBlock(动作)self.addTarget(self, action: "triggerActionHandleBlock", forControlEvents: control)}}

和电话:

 let button = UIButton()button.actionHandle(controlEvents: .touchUpInside,ForAction:{() ->无效打印(触摸")})

I have a generic control class which needs to set the completion of the button depending on the view controller.Due to that setLeftButtonActionWithClosure function needs to take as parameter a closure which should be set as action to an unbutton.How would it be possible in Swift since we need to pass the function name as String to action: parameter.

func setLeftButtonActionWithClosure(completion: () -> Void)
{
    self.leftButton.addTarget(<#target: AnyObject?#>, action: <#Selector#>, forControlEvents: <#UIControlEvents#>)
}

解决方案

Do Not Use This Answer, See Note Below

NOTE: like @EthanHuang said "This solution doesn't work if you have more than two instances. All actions will be overwrite by the last assignment." Keep in mind this when you develop, i will post another solution soon.

If you want to add a closure as target to a UIButton, you must add a function to UIButton class by using extension

Swift 5

import UIKit    
extension UIButton {
    private func actionHandler(action:(() -> Void)? = nil) {
        struct __ { static var action :(() -> Void)? }
        if action != nil { __.action = action }
        else { __.action?() }
    }   
    @objc private func triggerActionHandler() {
        self.actionHandler()
    }   
    func actionHandler(controlEvents control :UIControl.Event, ForAction action:@escaping () -> Void) {
        self.actionHandler(action: action)
        self.addTarget(self, action: #selector(triggerActionHandler), for: control)
    }
}

Older

import UIKit

extension UIButton {
    private func actionHandleBlock(action:(() -> Void)? = nil) {
        struct __ {
            static var action :(() -> Void)?
        }
        if action != nil {
            __.action = action
        } else {
            __.action?()
        }
    }
    
    @objc private func triggerActionHandleBlock() {
        self.actionHandleBlock()
    }
    
    func actionHandle(controlEvents control :UIControlEvents, ForAction action:() -> Void) {
        self.actionHandleBlock(action)
        self.addTarget(self, action: "triggerActionHandleBlock", forControlEvents: control)
    }
}

and the call:

 let button = UIButton()
 button.actionHandle(controlEvents: .touchUpInside, 
 ForAction:{() -> Void in
     print("Touch")
 })

这篇关于将闭包作为目标添加到 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆