13

I am using storyboards in my application, and if I set the title property of a ViewController class, that string will appear as the text of my back button when I push a SecondViewController, how can I change this?, I want to put a different title for the back button.

I have used this but it doesn't work:

UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back" 
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:nil];
    [self.navigationItem setBackBarButtonItem: btnBack];

please help me

CarinaM
  • 527
  • 4
  • 12
  • 27

5 Answers5

49

Try this ,

Objective-C:

UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                            initWithTitle:@"Done"
                            style:UIBarButtonItemStyleBordered
                            target:self
                            action:nil];
self.navigationController.navigationBar.topItem.backBarButtonItem=btnBack;

Swift:

var btn = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "backBtnClicked")

self.navigationController?.navigationBar.topItem?.backBarButtonItem=btn
Kalpesh
  • 5,336
  • 26
  • 41
  • 1
    @Nick Setting self.navigationItem will not work. Should set the backBarButtonItem to the previous viewController navigationItem. This answer only work in first time `viewWillAppear`, because `navigationBar.topItem` is still pointing to the `previousViewController.navigationItem`. After 1st time viewWillAppear, should set to the `backItem`. Please check [my answer](http://stackoverflow.com/a/25680043/111277). – situee Oct 16 '14 at 03:30
  • If your App is using Storyboards, there is a quick way to do this with no need for any code to be added to any controller. See the [answer](http://stackoverflow.com/a/25485293/1136076) below if you are using Storyboards. – Ste Prescott Apr 16 '15 at 12:42
12

When you use Storyboards you can change the text of the left bar button item aka the back button with no code. You will obviously need a UINavigationController within your Storyboard so you have the ability to push and pop views.

  1. Select the Navigation item object for the source view controller
  2. Enter text into the Back Button option in the inspector view on the right.

Then when a view is pushed from this view it will use this title set.

enter image description here

Ste Prescott
  • 1,789
  • 2
  • 22
  • 43
1

Set a backBarButtonItem to the navigationItem of the previous viewController. Setting the topItem only works on the first time viewWillAppear. Please check this post for detail: iOS Set Navigation Bar Back Button Title

NSArray *viewControllerArray = [self.navigationController viewControllers];
// get index of the previous ViewContoller
long previousIndex = [viewControllerArray indexOfObject:self] - 1;
if (previousIndex >= 0) {
    UIViewController *previous = [viewControllerArray objectAtIndex:previousIndex];
    previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                                     initWithTitle:backButtonTitle
                                                     style:UIBarButtonItemStylePlain
                                                     target:self
                                                     action:nil];
}
situee
  • 2,710
  • 2
  • 22
  • 27
0

Try changing the title of the first view controller where you push the second view controller, and set the tittle to the old one when u pop.

pradeepa
  • 4,104
  • 5
  • 31
  • 41
-2

Try this:

    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                       initWithTitle:@"Back" 
                                       style:UIBarButtonItemStyleBordered
                                       target:self
                                       action:@selector(backButtonClicked:)];
        [self.navigationItem setLeftBarButtonItem: btnBack];


   -(void)backButtonClicked:(UIBarButtonItem*)sender
     {
        //do ur stuff
     }
Satish Azad
  • 2,302
  • 1
  • 16
  • 35