iOS navigationItem.rightBarButtonItem 没有响应它的选择器
iOS navigationItem.rightBarButtonItem did not respone to its selector
我有一个继承自 A 的超级 class A 和一个子 classb B。我想展示 B。当在 class 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 {
}
我认为出于某种原因,当 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
}
}
我有一个继承自 A 的超级 class A 和一个子 classb B。我想展示 B。当在 class 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 {
}
我认为出于某种原因,当 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
}
}