简体   繁体   English

是否有与UIButton上的addTarget不同的选项

[英]Is there a different option than addTarget on a UIButton

addTarget addTarget

So I know that you can use addTarget on a button like so: 所以我知道您可以在按钮上使用addTarget,如下所示:

import UIKit

class ViewController: UIViewController {
    func calledMethod(_ sender: UIButton!) {
        print("Clicked")
    }

     override func viewDidLoad() {
        super.viewDidLoad()

        let btn = UIButton(frame: CGRect(x: 30, y: 30, width: 60, height: 30))
        btn.backgroundColor = UIColor.darkGray
        btn.addTarget(self, action: #selector(self.calledMethod(_:)), for: UIControlEvents.touchUpInside)
        self.view.addSubview(btn)
    }
}

This works absolutely fine but I was just wondering is there another way to detect when something is clicked and run some code when that happens. 这绝对可以正常工作,但我只是想知道是否还有另一种方法可以检测何时单击某项,并在发生这种情况时运行一些代码。

You can add button in the storyboard and create an outlet for it's action. 您可以在情节提要中添加按钮并为其动作创建出口。

@IBAction func buttonAction(_ sender: UIButton) {
  //implement your code here
}

You can also have a look at RxSwift/RxCocoa or similar. 您也可以看看RxSwift / RxCocoa或类似的东西。 All the changes and actions are added automatically and you can decide if you want to observe them or not. 所有更改和操作都会自动添加,您可以决定是否要观察它们。

The code would look something like this: 代码看起来像这样:

   let btn = UIButton(frame: CGRect(x: 30, y: 30, width: 60, height: 30))
   btn.backgroundColor = UIColor.darkGray
   btn
      .rx
      .tap
      .subscribe(onNext: {
          print("Clicked")
      }).disposed(by: disposeBag)
   view.addSubview(btn)    

A button is a subclass of UIControl . 按钮是UIControl的子类。 A UIControl is designed to use target/action to specify code to run when the specified action occurs. UIControl设计为使用目标/动作来指定发生指定动作时要运行的代码。 If you don't want to use that mechanism, you have to create an equivalent. 如果您不想使用该机制,则必须创建一个等效项。

You could attach a tap gesture recognizer to some other object and set it's userInteractionEnabled flag to true, but gesture recognizers ALSO use the target/action pattern. 你可以附上敲击手势识别其他一些对象,并设置它的userInteractionEnabled标志设置为true,但手势识别使用目标/行动模式。

My suggestion is to "stop worrying and learn to love the button." 我的建议是“不要担心,要学会爱按钮”。 Just learn to use target/action. 只是学习使用目标/动作。

Another possibility: Create a custom subclass of UIButton that sets itself up as it's own target at init time (specifically in init(coder:) and init(frame:) , the 2 init methods you need to implement for view objects.) This button would include an array of closures that its action method would execute if the button was tapped. 另一种可能性:创建UIButton的自定义子类,该子类在初始化时将其自身设置为自己的目标(特别是init(coder:)init(frame:) ,这是您需要为视图对象实现的2种init方法。)将包含一个闭包数组,如果点击该按钮,闭包数组的动作方法将执行该闭包。 It would have methods to add or remove closures. 它将具有添加或删除闭包的方法。 You'd then put such a button in your view controller and call the method to add the closure you want. 然后,您需要在视图控制器中放置一个这样的按钮,然后调用该方法以添加所需的闭包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM