60

I have a UIViewController and I'm navigating from my first view controller to second view controller and I want to change the name of the button shown on the navigationcontroller for going back ....

SecondViewController *secondController = [[SecondViewController alloc]
                                              initWithNibName:nil
                                              bundle:NULL]; 
[self.navigationController pushViewController:secondController  animated:YES];

now in the second viewcontroller I want change the name of the button in the navigationController.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Bulla
  • 924
  • 1
  • 7
  • 15

18 Answers18

185

If you wish to do this programmatically, it can be done like this:

Objective-C

UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"Custom"
                                                             style:UIBarButtonItemStyleBordered
                                                            target:nil
                                                            action:nil];

[self.navigationItem setBackBarButtonItem:backItem];

Swift

let backItem = UIBarButtonItem(title: "Custom", style: .Bordered, target: nil, action: nil)
navigationItem.backBarButtonItem = backItem

However, if you prefer using Interface Builder, just select the UINavigationItem that you wish to set the back button for, and navigate to the attributes inspector to change the back button's title.

enter image description here

NOTE: It is important to remember that you must configure this on the view controller that you would be returning to upon tapping the back button, not the currently visible view controller.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • 1
    @Shady It's still there. Just select your UINavigationItem and you will see it in the "Attributes Inspector" tab in Interface Builder. – Mick MacCallum Nov 19 '13 at 11:38
  • 11
    Thanks for this. Especially for the note in the end. – palme Jan 28 '14 at 12:49
  • You can combine the two approaches, programmatic and with storyboards. Add a dummy backButton title (as in the image shown) to the previous `viewController` in the hierarchy (so a backButton exists) and write `self.navigationItem.backBarButtonItem.title = @"...";` to set the back title for the next `viewController` – Iree Mar 06 '15 at 18:58
  • Also, in addition to the last note of putting it on the view controller that you are returning to, you need to also not have set the "Title" attribute, otherwise it will override the "Back Button" attribute. – Ben L. May 22 '15 at 13:13
  • 3
    The note is very important, should be with a higher font size and at the beginning of the answer – sports Jun 03 '15 at 04:52
  • 3
    since iOS8 `UIBarButtonItemStyleBordered` was deprecated and now `UIBarButtonItemStylePlain` should be used instead – drpawelo Jul 08 '15 at 14:15
  • 2
    The note is very important! – chengsam Sep 28 '16 at 02:36
  • 1
    thanks for this, accepted answer seems bit hacky, this actually should be the accepted answer – AamirR Dec 16 '16 at 16:42
42

There is an easy way of doing that in Interface Builder or Storyboarding. In the parent View Controller, just set the attribute "Back Button" of the navigation item to whatever title you want. Here is an example:

Set Back Button Title of child view controller form parent view controller

Villian's Tavern View will have "Back" as Back Button Title, like we just set in the attributes inspector of the parent's navigation controller (Best Bars).

Roger
  • 911
  • 2
  • 9
  • 15
  • You may have to add a navigation item to your view controller on the storyboard if it's not already there. To do that, select UINavigationItem from the object library and drag it onto your view controller on the story board. – David Green Sep 12 '16 at 14:54
35

In viewWillAppear write this

self.navigationItem.title = @"List View";

And in ViewWilldisapper write this

self.navigationItem.title = @"Back";

It works without story boards.

Tendulkar
  • 5,550
  • 2
  • 27
  • 53
  • 2
    Done this way you will see the title actually change before the new view (especially if you have animation). If you do it with viewDidDisappear, then you will see the button actually change. NSPostWhenIdle's solution is better. Originally sourced from: http://blog.evandavey.com/2008/12/how-to-change-text-on-back-button-in-uinavigationbar-uinavigationcontroller.html – Καrτhικ Aug 14 '12 at 17:21
  • 15
    This should not be the accepted answer. 0x7fffffff's answer is the supported method for doing this. – Brody Robertson May 16 '13 at 04:09
  • 2
    This does not work anymore. It's also very hacky. The answer of user716216 [http://stackoverflow.com/a/9871741/1195661] should be the right one. – palme Jan 28 '14 at 12:51
  • I have tried all the solutions but the only one works flawlessly everywhere is adding a bar button to navigation bar in designer or through code and set the custom selector manually believe me this is just three four lines of task but saves a lot of frustration – vishal dharankar Feb 09 '14 at 08:03
  • 2
    This is a not a great answer because it is effectively working around – not with – the provided UIKit facilities for view controller bar buttons. see answer from @0x7ffffffff below – Adam Kaplan Jan 11 '16 at 20:49
  • i dont know why everyone is hating against this answer... it may not be best practise but it's working just fine... – David Seek Sep 01 '17 at 16:34
4

In my case it only worked running the code on the Main Thread, like so:

dispatch_async(dispatch_get_main_queue(), ^(void) {

    self.navigationController.navigationBar.backItem.title = @"Custom Title";
});
neowinston
  • 7,584
  • 10
  • 52
  • 83
4

Swift 3:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Back"
    navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
}

Note:The back button belongs to the previous view controller, not the one currently presented on screen. To modify the back button you should update it before pushing, on the view controller that initiated the segue:

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Anil
  • 272
  • 3
  • 14
3

In Swift I found solution very simple,

Suppose I'm on ViewControllerA and going to ViewControllerB, If I wants to change name of back button showing on ViewControllerB, I will do this in ViewControllerA,

self.title = "BackButtonTitle"

That's it.

Note:- This will change title of ViewControllerA

Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
3

I'm on ViewController1 and going to ViewController2, If I wants to change name of back button showing on ViewController2, I will do this in ViewController2.

Swift 5

self.navigationController?.navigationBar.topItem?.title = "BackButtonTitle"

Objective-C

self.navigationController.navigationBar.topItem.title = @"BackButtonTitle";
said altintop
  • 119
  • 1
  • 4
1

It is very important that in view controller "Simulated Metrics" option is not selected as "Inferred".

I tried with "None" and all it's right!

Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79
1

Try like this:-

NSArray *viewControllerArr = [self.navigationController viewControllers];
// get index of the previous ViewContoller
long previousViewControllerIndex = [viewControllerArr indexOfObject:self] - 1;
UIViewController *previous;
if (previousViewControllerIndex >= 0) {
    previous = [viewControllerArr objectAtIndex:previousViewControllerIndex];
    previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                                 initWithTitle:@"Back"
                                                 style:UIBarButtonItemStylePlain
                                                 target:self
                                                 action:nil];
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
1

Actually, you can change title of back button:

self.navigationItem.backBarButtonItem.title = @"NewName";
Pang
  • 9,564
  • 146
  • 81
  • 122
1

If you use Storyboard references, the View Controller might not display the Navigation Item on Interface Builder.

Manually dragging a Navigation Bar will not work, you need to drag a Navigation Item

You'll then be able to set its back button's title. This is also where you'll set the view's title.

Quoting Mick MacCallum:

NOTE: It is important to remember that you must configure this on the view controller that you would be returning to upon tapping the back button, not the currently visible view controller.

Lucas
  • 6,675
  • 3
  • 25
  • 43
1

You can achieve this by using titleView of navigationItem

Swift 3 Answer

• Create one extension for UINavigationItem as Below

extension UINavigationItem {
    func customTitle(title: String) {
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.textColor = UIColor.white
        titleLabel.textAlignment = .center
        titleLabel.sizeToFit()
        titleView = titleLabel
    }
}

• In your controllers viewDidLoad do the following

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "Text For Back Button"
        navigationItem.customTitle(title: "Title for your view controller")
}
Leojin
  • 31
  • 5
1

Try Below Code :

[self.navigationItem setValue:@[@"customTitle",@""] forKey:@"abbreviatedBackButtonTitles"];
LuFFy
  • 8,799
  • 10
  • 41
  • 59
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/18409675) – 31piy Jan 03 '18 at 05:53
  • Thanks for your advice, this is my second time to answer one question, I will learn more about the rules, and I will correct it next time. – CharmingQin Jan 03 '18 at 08:05
0

If you use Storyboard, there is a easy way to do this (it worked fine for me).

  • Go to your storyboard
  • Select which UIViewController you want to change the back button text.
  • Click on the UINavigationItem on the top of your UIViewController. (the one with the title)
  • On the Right panel, select the attribute inspector (the 4th icon).
  • Now you can set the Title, Prompt and Back Button texts.

enter image description here

Source: http://pinkstone.co.uk/how-to-change-the-back-button-text-on-a-uinavigationcontroller-in-your-storyboard/

Luis
  • 709
  • 8
  • 10
0

There are two ways to do this.

First way is by program code:

Yakimenko Aleksey's answer is the simplest way, This really works ( tested on Xcode 8 + iOS 10)

Swift3:

self.navigationItem.backBarButtonItem?.title = "Your customized back title"

Note: you should call above code in source view controller, not the destination view controller.

Second way is use storyboard localization via Main.strings file,

"nnnnnnn.title" = "localized string";

The nnnnnnn means the object id of the BackBarButtonItem in the source view controller(not the destination view controller!)

You need set the "back" property of the navigation bar item, it will auto create a BarButtonItem, you find its object id.

Sorry, i failed to upload screenshots.

osexp2000
  • 2,910
  • 30
  • 29
0

I had a rather complex storyboard with many navigation controllers, and my customer wanted all of the back buttons to say "Back"

I went through in Interface Builder and put "Back" into the Back button field of every UINavigationItem.

I was not working for every scene change!

The problem was that not every view controller in the storyboard had a UINavigationItem.

I fixed this by dragging out a new UINavigationItem onto every UIViewController that did not have one, and setting all of the Back button fields to "Back"

Watch out for the controllers that are not given their own UINavigationItem automatically.

DrSmart
  • 381
  • 2
  • 5
0

Swift 4

In your UIViewController, set this:

let backButton = UIBarButtonItem()
backButton.title = "[Your title here]"
self.navigationItem.backBarButtonItem = backButton
Ben-hur Ott
  • 189
  • 1
  • 1
  • 8
0

In Xcode 12 UINavigationItem has the backButtonTitle property

var backButtonTitle: String? { get set }
Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93