<tfoot id='aRoMg'></tfoot>
      <legend id='aRoMg'><style id='aRoMg'><dir id='aRoMg'><q id='aRoMg'></q></dir></style></legend>

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

      1. <small id='aRoMg'></small><noframes id='aRoMg'>

      2. <i id='aRoMg'><tr id='aRoMg'><dt id='aRoMg'><q id='aRoMg'><span id='aRoMg'><b id='aRoMg'><form id='aRoMg'><ins id='aRoMg'></ins><ul id='aRoMg'></ul><sub id='aRoMg'></sub></form><legend id='aRoMg'></legend><bdo id='aRoMg'><pre id='aRoMg'><center id='aRoMg'></center></pre></bdo></b><th id='aRoMg'></th></span></q></dt></tr></i><div id='aRoMg'><tfoot id='aRoMg'></tfoot><dl id='aRoMg'><fieldset id='aRoMg'></fieldset></dl></div>
      3. Swift 以编程方式为带有闭包的按钮创建函数

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

            <bdo id='Y1Yjb'></bdo><ul id='Y1Yjb'></ul>

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

                    <tbody id='Y1Yjb'></tbody>
                1. <small id='Y1Yjb'></small><noframes id='Y1Yjb'>

                2. <tfoot id='Y1Yjb'></tfoot>
                  本文介绍了Swift 以编程方式为带有闭包的按钮创建函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Swift 中,您可以像这样为按钮创建一个函数:

                  In Swift you can create a function for a button like this:

                  button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
                  

                  但是有办法让我做这样的事情:

                  However is there a way I can do something like this:

                  button.whenButtonIsClicked({Insert code here})
                  

                  这样我什至没有为按钮声明一个显式函数.我知道我可以使用按钮标签,但我更愿意这样做.

                  That way I do not even have too declare an explicit function for the button. I know I can use button tags but I would prefer to do this instead.

                  推荐答案

                  创建你自己的 UIButton 子类来做到这一点:

                  Create your own UIButton subclass to do this:

                  class MyButton: UIButton {
                      var action: (() -> Void)?
                  
                      func whenButtonIsClicked(action: @escaping () -> Void) {
                          self.action = action
                          self.addTarget(self, action: #selector(MyButton.clicked), for: .touchUpInside)
                      }
                  
                      // Button Event Handler:
                      // I have not marked this as @IBAction because it is not intended to
                      // be hooked up to Interface Builder       
                      @objc func clicked() {
                          action?()
                      }
                  }
                  

                  当您以编程方式创建按钮然后调用 whenButtonIsClicked 来设置其功能时,用 MyButton 替换 UIButton.

                  Substitute MyButton for UIButton when you create buttons programmatically and then call whenButtonIsClicked to set up its functionality.

                  您也可以在情节提要中将其与 UIButton 一起使用(只需将其类更改为 MyButton),然后在 中调用 whenButtonIsClicked>viewDidLoad.

                  You can also use this with UIButtons in a Storyboard (just change their class to MyButton) and then call whenButtonIsClicked in viewDidLoad.

                  @IBOutlet weak var theButton: MyButton!
                  
                  var count = 0
                  
                  override func viewDidLoad() {
                      super.viewDidLoad()
                  
                      // be sure to declare [unowned self] if you access
                      // properties or methods of the class so that you
                      // don't create a strong reference cycle
                      theButton.whenButtonIsClicked { [unowned self] in
                          self.count += 1
                          print("count = (self.count)")
                      }
                  

                  <小时>

                  更强大的实现

                  认识到程序员可能想要处理更多事件而不仅仅是 .touchUpInside,我编写了这个功能更强大的版本,它支持每个 UIButton 多个闭包和每个事件多个闭包输入.

                  Recognizing the fact that programmers might want to handle more events than just .touchUpInside, I wrote this more capable version which supports multiple closures per UIButton and multiple closures per event type.

                  class ClosureButton: UIButton {
                      private var actions = [UInt : [((UIControl.Event) -> Void)]]()
                  
                      private let funcDict: [UInt : Selector] = [
                          UIControl.Event.touchCancel.rawValue:       #selector(eventTouchCancel),
                          UIControl.Event.touchDown.rawValue:         #selector(eventTouchDown),
                          UIControl.Event.touchDownRepeat.rawValue:   #selector(eventTouchDownRepeat),
                          UIControl.Event.touchUpInside.rawValue:     #selector(eventTouchUpInside),
                          UIControl.Event.touchUpOutside.rawValue:    #selector(eventTouchUpOutside),
                          UIControl.Event.touchDragEnter.rawValue:    #selector(eventTouchDragEnter),
                          UIControl.Event.touchDragExit.rawValue:     #selector(eventTouchDragExit),
                          UIControl.Event.touchDragInside.rawValue:   #selector(eventTouchDragInside),
                          UIControl.Event.touchDragOutside.rawValue:  #selector(eventTouchDragOutside)
                      ]
                  
                      func handle(events: [UIControl.Event], action: @escaping (UIControl.Event) -> Void) {
                          for event in events {
                              if var closures = actions[event.rawValue] {
                                  closures.append(action)
                                  actions[event.rawValue] = closures
                              } else {
                                  guard let sel = funcDict[event.rawValue] else { continue }
                                  self.addTarget(self, action: sel, for: event)
                                  actions[event.rawValue] = [action]
                              }
                          }
                      }
                  
                      private func callActions(for event: UIControl.Event) {
                          guard let actions = actions[event.rawValue] else { return }
                          for action in actions {
                              action(event)
                          }
                      }
                  
                      @objc private func eventTouchCancel()       { callActions(for: .touchCancel) }
                      @objc private func eventTouchDown()         { callActions(for: .touchDown) }
                      @objc private func eventTouchDownRepeat()   { callActions(for: .touchDownRepeat) }
                      @objc private func eventTouchUpInside()     { callActions(for: .touchUpInside) }
                      @objc private func eventTouchUpOutside()    { callActions(for: .touchUpOutside) }
                      @objc private func eventTouchDragEnter()    { callActions(for: .touchDragEnter) }
                      @objc private func eventTouchDragExit()     { callActions(for: .touchDragExit) }
                      @objc private func eventTouchDragInside()   { callActions(for: .touchDragInside) }
                      @objc private func eventTouchDragOutside()  { callActions(for: .touchDragOutside) }
                  }
                  

                  演示

                  class ViewController: UIViewController {
                  
                      var count = 0
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                  
                          let button = ClosureButton(frame: CGRect(x: 50, y: 100, width: 60, height: 40))
                          button.setTitle("press me", for: .normal)
                          button.setTitleColor(.blue, for: .normal)
                  
                          // Demonstration of handling a single UIControl.Event type.
                          // If your closure accesses self, be sure to declare [unowned self]
                          // to prevent a strong reference cycle
                          button.handle(events: [.touchUpInside]) { [unowned self] _ in
                              self.count += 1
                              print("count = (self.count)")
                          }
                  
                          // Define a second handler for touchUpInside:
                          button.handle(events: [.touchUpInside]) { _ in
                              print("I'll be called on touchUpInside too")
                          }
                  
                          let manyEvents: [UIControl.Event] = [.touchCancel, .touchUpInside, .touchDown, .touchDownRepeat, .touchUpOutside, .touchDragEnter,
                               .touchDragExit, .touchDragInside, .touchDragOutside]
                  
                          // Demonstration of handling multiple events
                          button.handle(events: manyEvents) { event in
                              switch event {
                              case .touchCancel:
                                  print("touchCancel")
                              case .touchDown:
                                  print("touchDown")
                              case .touchDownRepeat:
                                  print("touchDownRepeat")
                              case .touchUpInside:
                                  print("touchUpInside")
                              case .touchUpOutside:
                                  print("touchUpOutside")
                              case .touchDragEnter:
                                  print("touchDragEnter")
                              case .touchDragExit:
                                  print("touchDragExit")
                              case .touchDragInside:
                                  print("touchDragInside")
                              case .touchDragOutside:
                                  print("touchDragOutside")
                              default:
                                  break
                              }
                          }
                  
                          self.view.addSubview(button)
                      }
                  }
                  

                  这篇关于Swift 以编程方式为带有闭包的按钮创建函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:自定义按钮框架看起来不如圆形矩形 UIButton 下一篇:如何在uisearchbar中始终显示Xbutton(清除按钮)

                  相关文章

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

                        <bdo id='bVpYi'></bdo><ul id='bVpYi'></ul>
                      <tfoot id='bVpYi'></tfoot>

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