繁体   English   中英

为什么addTarget在iOS的子视图中不起作用?

[英]Why addTarget isn't working in subview on iOS?

子视图中的addTarget无法正常工作,仅在第一次点击按钮时有效。

屏幕截图

测试控制器

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")
    }
}

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

  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)

问题与视图框架大小有关当框架外部的按钮不起作用时,addTarget不起作用

暂无
暂无

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

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