0

Hi I've been trying to change my back button in my application's navigation bar. The problem is that I see the change when I load the view twice.

I've searched for the answer several times but I don't see what I really want. In fact, I'm new at this language so it's difficult.

What I tried is putting the following lines in the viewWillAppear method and others*:

nav?.navigationBar.backItem?.title = "Messages"

The result is that when I enter the view I see the back button's title as Back. Then if I press on that button and enter the view again the title changes as I want. On the other hand, what I want is to change the title when I load the view first.

*-> I've tried the same line in viewDidLoad too see if that does anything and in viewWillDisappear of the previous view, but nothing happens.

Revanth Kausikan
  • 673
  • 1
  • 9
  • 21
  • You need to set it in the presenting viewController. If you're using `prepare for segue`, try `navigationItem.backBarButtonItem = UIBarButtonItem(title: "Messages", style: UIBarButtonItemStyle.plain, target: nil, action: nil)` – Don Mar 24 '19 at 03:19
  • Possible duplicate of [How do I change the title of the "back" button on a Navigation Bar](https://stackoverflow.com/questions/1449339/how-do-i-change-the-title-of-the-back-button-on-a-navigation-bar) – ManWithBear Mar 24 '19 at 03:31
  • you should set it from the parent view controller. If you are using storyboard then tap on the navigation bar of parent VC and change Back Button Attribute Inspector (right panel) – RJE Mar 24 '19 at 03:57
  • Hey @Don! I tried what you said but this time the button doesn't change anytime – Nicolás Villar Mar 24 '19 at 23:52

1 Answers1

1

You are using method a change back button in your current ViewController it is wrong, because your navigationBar configured in past controller. If you use Storyboard, please will try this method in your parent controller:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Messages"
    navigationItem.backBarButtonItem = backItem
}

If you init newController in your parentController, you must to modify your a backButton in parentController, example:

// It's action for any button witch present your newController

@objc private func presentDetailViewContorller() {
    let weatherDetailController = WeatherDetailController()
    let navController = NavigationController(rootViewController: weatherDetailController)
    let backItem = UIBarButtonItem()
    backItem.title = "Messages"
    navController.backBarButtonItem = backItem
    self.present(navController, animated: true, completion: nil)
}

Good luck!