我有点困惑。有两个UIViewControllers
在 UINavagationController
的堆栈。我尝试调用 的方法第一 UIViewController
内第二个 UIViewController
.见下面的代码
class VC1: UIViewController {
@objc func method1() {
//not called after tap button
}
}
class VC2: UIViewController {
let button = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(nil, action: #selector(VC1.method1), for: .touchUpInside)
}
}
但是当我点击按钮时不会调用方法1。有什么问题?
最佳答案
响应者链基于 super View 和父 View Controller 。所以 VC1 不在 VC2 的响应者链上。来自 VC2 的响应者链通向其父级(UINavigationController);它不会通过 UINavigationController 的任何其他子项。
关于swift - iOS 响应链和事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59974544/