1

When I assign the following UIBarButtonItem to backBarButtonItem I get a button with the title I assigned, but the action is never called. Using leftBarButtonItem instead, will add a button and the action will be called. What is overriding my action? Can I do something about it?

 override func viewDidLoad() {
        super.viewDidLoad()
        let backItem = UIBarButtonItem(title: "Back",
                                       style: .plain,
                                       target: self,
                                       action: #selector(backNavigationClick))
        navigationItem.backBarButtonItem = backItem
    }

    @objc func backNavigationClick() {
        print("back clicked")
    }
Jay Patel
  • 2,642
  • 2
  • 18
  • 40
Aviran
  • 5,160
  • 7
  • 44
  • 76

1 Answers1

-1

if you use it in this way you can customize the button.

You need to replace this code navigationItem.backBarButtonItem = backItem to navigationItem.leftBarButtonItem = backItem

and add this code on backNavigationClick()

@objc func backNavigationClick() {
    print("back clicked")
    self.navigationController?.popViewController(animated: true) ***
}
  • as I mentioned in the question, im aware it can be done using leftBarButtonItem, the problem with that is that you lose the default back arrow image. replacing it with a custom image is not what I want. – Aviran Jan 23 '18 at 15:37
  • if you change only back button title, use this code: self.navigationController?.navigationBar.backItem?.title = "Anything Else" or look this https://stackoverflow.com/a/44919649/8367837 – Ataberk Kocaman Jan 25 '18 at 13:12