为什么addTarget不在iOS上的子视图中工作?

时间:2018-02-18 12:03:36

标签: ios swift

子视图中的addTarget不起作用,仅使用第一个按钮点击。

screenshot

测试控制器

class ForTestsController: UIViewController {
    let view1 = UIView()
    let view2 = UIView()
    let view3 = UIView()
    let button = UIButton.createRectButton(title: "button")
    let button1 = UIButton.createRectButton(title: "button1")
    let button2 = UIButton.createRectButton(title: "button2")
    let button3 = UIButton.createRectButton(title: "button3")

    override func viewDidLoad() {
        view.addSubview(view1)
        view1.addSubview(view2)
        view2.addSubview(view3)

        view1.anchor(view.topAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: 100, leftConstant: 50, bottomConstant: 0, rightConstant: 0, widthConstant: view.frame.width, heightConstant: 300)
        view1.backgroundColor = .red

        view2.anchor(view1.topAnchor, left: view1.leftAnchor, bottom: nil, right: nil, topConstant: 100, leftConstant: 50, bottomConstant: 0, rightConstant: 0, widthConstant: view.frame.width - 50, heightConstant: 300)
        view2.backgroundColor = .green

        view3.anchor(view2.topAnchor, left: view2.leftAnchor, bottom: nil, right: nil, topConstant: 100, leftConstant: 50, bottomConstant: 0, rightConstant: 0, widthConstant: view.frame.width - 50, heightConstant: 300)
        view3.backgroundColor = .blue

        view.addSubview(button)
        view1.addSubview(button1)
        view2.addSubview(button2)
        view3.addSubview(button3)

        button.anchor(view.topAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: 30, leftConstant: 30, bottomConstant: 0, rightConstant: 0, widthConstant: 300, heightConstant: 60)

        button1.anchor(view1.topAnchor, left: view1.leftAnchor, bottom: nil, right: nil, topConstant: 30, leftConstant: 30, bottomConstant: 0, rightConstant: 0, widthConstant: 300, heightConstant: 60)
        button2.anchor(view2.topAnchor, left: view2.leftAnchor, bottom: nil, right: nil, topConstant: 30, leftConstant: 30, bottomConstant: 0, rightConstant: 0, widthConstant: 300, heightConstant: 60)
        button3.anchor(view3.topAnchor, left: view3.leftAnchor, bottom: nil, right: nil, topConstant: 30, leftConstant: 30, bottomConstant: 0, rightConstant: 0, widthConstant: 300, heightConstant: 60)

        button.addTarget(self, action: #selector(tap), for: .touchUpInside)
        button1.addTarget(self, action: #selector(tap), for: .touchUpInside)
        button2.addTarget(self, action: #selector(tap), for: .touchUpInside)
        button3.addTarget(self, action: #selector(tap), for: .touchUpInside)
    }

    @objc func tap() {
        print("tapped")
    }
}

2 个答案:

答案 0 :(得分:3)

您必须将目标添加到所有按钮才能工作

  button.addTarget(self, action: #selector(tap), for: .touchUpInside)
  button1.addTarget(self, action: #selector(tap), for: .touchUpInside)
  button2.addTarget(self, action: #selector(tap), for: .touchUpInside)
  button3.addTarget(self, action: #selector(tap), for: .touchUpInside)

答案 1 :(得分:0)

问题在于视图框架大小 当框架外的按钮时,addTarget不起作用