iOS navigationItem.rightBarButtonItem没有响应其选择器

时间:2018-05-23 02:32:44

标签: ios swift uinavigationcontroller uinavigationbar uinavigationitem

我有一个超级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 {

}

1 个答案:

答案 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
    }
}