present一个控制器出来后, 界面会响应前一个控制器touchesBegan方法.

新手上路,请多包涵

我在当前控制器A重写了touchesBegan方法.

present一个控制器B之后, 击B, 会触发A的touchesBegan方法.

class ViewController: UIViewController {
  
  let button = UIButton()

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    view.addSubview(button)
    button.frame = CGRect.init(x: 100, y: 100, width: 100, height: 100)
    button.backgroundColor = .red
    button.addTarget(self, action: #selector(present(_:)), for: .touchUpInside)
  }
  
  func present(_ sender: UIButton) {
    let vc = UIViewController()
    vc.view.backgroundColor = .green
    present(vc, animated: true, completion: nil)
  }
  
  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("123")
  }
}
阅读 3.1k
1 个回答

因为你说的vcB, let vcB = UIViewController(),
vcB和A是同一个类,有同样的方法,自然地界面会响应自己所属类的方法,也就是你说的"界面会响应前一个控制器touchesBegan方法".
根据你的提问, 我觉得 你应该新建一个类 ViewControllerB.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题