<legend id='yCAlT'><style id='yCAlT'><dir id='yCAlT'><q id='yCAlT'></q></dir></style></legend>

    <small id='yCAlT'></small><noframes id='yCAlT'>

    • <bdo id='yCAlT'></bdo><ul id='yCAlT'></ul>

    <i id='yCAlT'><tr id='yCAlT'><dt id='yCAlT'><q id='yCAlT'><span id='yCAlT'><b id='yCAlT'><form id='yCAlT'><ins id='yCAlT'></ins><ul id='yCAlT'></ul><sub id='yCAlT'></sub></form><legend id='yCAlT'></legend><bdo id='yCAlT'><pre id='yCAlT'><center id='yCAlT'></center></pre></bdo></b><th id='yCAlT'></th></span></q></dt></tr></i><div id='yCAlT'><tfoot id='yCAlT'></tfoot><dl id='yCAlT'><fieldset id='yCAlT'></fieldset></dl></div>

    1. <tfoot id='yCAlT'></tfoot>

      将闭包作为目标添加到 UIButton

      时间:2023-07-09
    2. <i id='yavSm'><tr id='yavSm'><dt id='yavSm'><q id='yavSm'><span id='yavSm'><b id='yavSm'><form id='yavSm'><ins id='yavSm'></ins><ul id='yavSm'></ul><sub id='yavSm'></sub></form><legend id='yavSm'></legend><bdo id='yavSm'><pre id='yavSm'><center id='yavSm'></center></pre></bdo></b><th id='yavSm'></th></span></q></dt></tr></i><div id='yavSm'><tfoot id='yavSm'></tfoot><dl id='yavSm'><fieldset id='yavSm'></fieldset></dl></div>
      <tfoot id='yavSm'></tfoot>

      <small id='yavSm'></small><noframes id='yavSm'>

          <tbody id='yavSm'></tbody>

          • <bdo id='yavSm'></bdo><ul id='yavSm'></ul>

          • <legend id='yavSm'><style id='yavSm'><dir id='yavSm'><q id='yavSm'></q></dir></style></legend>

                本文介绍了将闭包作为目标添加到 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何向 UIButton 添加多行文本? 下一篇:UIButton:如何使用 imageEdgeInsets 和 titleEdgeInsets 使图像和文本居中?

                相关文章

              1. <i id='pG8zj'><tr id='pG8zj'><dt id='pG8zj'><q id='pG8zj'><span id='pG8zj'><b id='pG8zj'><form id='pG8zj'><ins id='pG8zj'></ins><ul id='pG8zj'></ul><sub id='pG8zj'></sub></form><legend id='pG8zj'></legend><bdo id='pG8zj'><pre id='pG8zj'><center id='pG8zj'></center></pre></bdo></b><th id='pG8zj'></th></span></q></dt></tr></i><div id='pG8zj'><tfoot id='pG8zj'></tfoot><dl id='pG8zj'><fieldset id='pG8zj'></fieldset></dl></div>
              2. <tfoot id='pG8zj'></tfoot>

                    <bdo id='pG8zj'></bdo><ul id='pG8zj'></ul>
                  1. <legend id='pG8zj'><style id='pG8zj'><dir id='pG8zj'><q id='pG8zj'></q></dir></style></legend>
                  2. <small id='pG8zj'></small><noframes id='pG8zj'>