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