1

I know that you can set the title of the back button from the IB or in prepareForSegue, but in my app I need to update the title according to events that can happen while the controller is visible.

I tried but nothing happens:

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: title, style: .Plain, target: nil, action: nil)

This works though but has no back arrow:

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, style: .Plain, target: nil, action: "popVC")

Any ideas?

MJQZ1347
  • 2,607
  • 7
  • 27
  • 49

3 Answers3

2

The backBarButtonItem is the item used for the back button of the next controller in the navigation stack.
So for example if you have a navigation controller with a root viewController A and you push a viewController B, then the back button title that you see once B is pushed is configured using A.

You could have something like this :

A.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Go back to A", style: .Plain, target: nil, action: nil)

Once B is pushed, you see a back button with "Go back to A".

In your case the tricky part is to find A in the navigation stack from B. You can do it by searching in the viewControllers of the navigationController like so :

// This code works from the `B` view controller
let viewControllers = self.navigationController?.viewControllers ?? []
if let indexOfCurrent = viewControllers.indexOf(self) where (indexOfCurrent > viewControllers.startIndex) {
    let indexOfPrevious = indexOfCurrent.advancedBy(-1)
    let previousViewController = viewControllers[indexOfPrevious]
    previousViewController.navigationItem.backBarButtonItem?.title = "New Title"
}

Edit

I don't know any clean way to refresh the navigation bar after that. Maybe you could ask a separate question just for that.
What you could do is pop and push the view controller without animation

if let navigationController = self.navigationController {
    navigationController.popViewControllerAnimated(false)
    navigationController.pushViewController(self, animated: false)
}

Or maybe try to create a new UIBarButtonItem instead of changing the title of the existing one.

deadbeef
  • 5,409
  • 2
  • 17
  • 47
  • But as drawn out in my queastion the back button title can change after the B is pushed. It should work like WhatsApp where the back button in the chat view controller gets updated with the number of newly arrived chat messages. I am curious how they managed to do it? – MJQZ1347 Aug 21 '16 at 15:07
  • Thanks, tried that. It seems to work, but only after `B` has been poped and opend again. Is there a way to refresh the `navigationBar`? – MJQZ1347 Aug 21 '16 at 15:12
  • The refreshing works, thanks! BTW: Why not just use `navigationController?.popViewControllerAnimated(false); navigationController?.pushViewController(self, animated: false)` instead of the optional binding? – MJQZ1347 Aug 21 '16 at 15:21
  • Regarding creating a new `UIBarButtonItem`: If a new one is created it misses the back arrow... – MJQZ1347 Aug 21 '16 at 15:30
0

try this:

let baritems = self.navigationController?.navigationBar.items

        for item in baritems!{
            if (item.leftBarButtonItem != nil){
                item.title = "123"
            }
        }
Dasem
  • 440
  • 3
  • 11
0

This question is pretty old now, but I was a problem changing the back button text based on a nested tableViewController's selected cell.

It looks like the default back button for a navigationViewController bases its text on the title of the view controller it takes the user back to.

You should probably be careful, as my solution makes a couple assumptions that I'm not positive will always be true.

let vcs = navigationController?.viewControllers
vcs?[(vcs?.count)! - 2].navigationItem.title = "Your Text Here"

Assuming that our current view controller is at the top of the navigation stack -> vcs[vcs.count - 1]; there exists a view controller on that stack before this one -> vcs[vcs.count - 2]; and the navigationItem.title of that preceding view controller can be changed without unwanted side effects; we can change our back button text by changing the navigation title of that preceding view controller.