我有一个超级A和一个继承自A的子类B.我想呈现B.当在类A中添加navigationItem.rightBarButtonItem时,选择器没有响应它的选择器。
class A: UIViewController {
var saveNaviBtn: UIBarButtonItem = UIBarButtonItem(title: "save", style: .done, target: self, action: #selector(save))
@objc func save() {
dismiss(animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = saveNaviBtn
}
}
class B: A {
}
答案 0 :(得分:1)
我认为出于某种原因,当barButtonItem初始化时,它的目标self
可能无法完成。这样做:
class A: UIViewController {
var saveNaviBtn: UIBarButtonItem?
override func viewDidLoad() {
saveNaviBtn = UIBarButtonItem(title: "save", style: .done, target: self, action: #selector(save))
self.navigationItem.rightBarButtonItem = saveNaviBtn
}
}