具有多个目标操作的 UIButton 同一事件 [英] UIButton with multiple target actions for same event

查看:25
本文介绍了具有多个目标操作的 UIButton 同一事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 UIButton 上为相同的事件添加多个目标操作吗?

[button addTarget:self action:@selector(xxx) forControlEvents:UIControlEventTouchUpInside];[button addTarget:object action:@selector(yyy) forControlEvents:UIControlEventTouchUpInside];

我制作了一个快速应用程序来测试它,它在按下按钮时执行两种操作.

我想知道这样做是否是好的做法,并且执行顺序是否始终保持不变?

提前致谢.

我确实找到了这个帖子 表示以与添加相反的顺序调用它,即首先调用最近添加的目标.但未证实

解决方案

是的,可以向一个按钮添加多个操作.

就我个人而言,我更希望委托人订阅该按钮.让您想要添加为 targetobject 订阅委托的方法,以便在您按下按钮时它可以接收事件.

将事件转发到其他方法以完全控制的单个操作

一个简单的快速测试

导入 UIKit类视图控制器:UIViewController {覆盖 func viewDidLoad() {super.viewDidLoad()//加载视图后进行任何其他设置.let button = UIButton(frame: CGRect(x: 50, y: 50, width: 300, height: 30))button.backgroundColor = .orangebutton.addTarget(self, action: #selector(action1), for: .touchUpInside)button.addTarget(self, action: #selector(action2), for: .touchUpInside)button.addTarget(self, action: #selector(actionHandler), for: .touchUpInside)self.view.addSubview(按钮)}@objc func actionHandler(_ sender: UIButton){打印(actionHandler")动作1(发件人)动作2(发送者)}@objc func action1(_ sender: UIButton) {打印(动作1")}@objc func action2(_ sender: UIButton) {打印(action2 \n")}}

一键输出:

action1行动2动作处理程序动作1行动2

<块引用>

能否确认正常添加动作时的执行顺序

是的,它是按照您设置的目标顺序执行的.

Can I add multiple target actions on a UIButton for the same event like below?

[button addTarget:self action:@selector(xxx) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:object action:@selector(yyy) forControlEvents:UIControlEventTouchUpInside];

I made a quick app to test it out and it carries out both the actions on button press.

I want to know if it's good practice to do so, and also will the order of execution always remain the same?

Thanks in advance.

Edit: I did find this post which states it's called in reverse order of addition, i.e., the most recently added target is called first. But it's not confirmed

解决方案

Yes it is possible to add multiple actions to a button.

personally i would prefer a Delegate to subscribe to the button. Let the object you want to add as target subscribe on the delegate's method so it can receive events when you press the button.

or

A single action that forwards the event to other methods to be fully in control

A simple test in swift

import UIKit

class ViewController: UIViewController {

  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view.

      let button = UIButton(frame: CGRect(x: 50, y: 50, width: 300, height: 30))
      button.backgroundColor = .orange
      button.addTarget(self, action: #selector(action1), for: .touchUpInside)
      button.addTarget(self, action: #selector(action2), for: .touchUpInside)
      button.addTarget(self, action: #selector(actionHandler), for: .touchUpInside)
      self.view.addSubview(button)
  }

  @objc func actionHandler(_ sender: UIButton){
      print("actionHandler")
      action1(sender)
      action2(sender)
  }

  @objc func action1(_ sender: UIButton) {
      print("action1")
  }

  @objc func action2(_ sender: UIButton) {
      print("action2 \n")
  }
}

Output after one click:

action1
action2 

actionHandler
action1
action2 

Can you confirm on the order of execution when normally adding the actions

Yes it is executed in the order you set the targets.

这篇关于具有多个目标操作的 UIButton 同一事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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