5

I created one title for navigationbar in my storyboard project ,but when I moved to next viewcontroller the back button shows the same name as my previous navigationbar(Main navigationbar) title.? Can i set separate title and back button names?

I tried following code but its not working ? why?

 self.navigationItem.title=@"Recent Books";
self.navigationItem.backBarButtonItem.title=@"Back";
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
Naveen
  • 1,251
  • 9
  • 20
  • 38

5 Answers5

19

According to UINavigationItem Class Reference

"When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. [...] If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead."

So try this in your first VC from where you are pushing other VC

self.navigationItem.title=@"Recent Books";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back"
                                   style:UIBarButtonItemStylePlain
                                   target:nil
                                   action:nil];
self.navigationItem.backBarButtonItem=backButton;
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
  • I have two BarButton customized on my first view, on the left to "Information" and the right to "Settings". And yet, I had to set a name for BackButton in the first view to. In my component tree now are three BarButton, but when I run the app everything worked normally. Simply makes no sense. – Douglas Nassif Roma Junior May 25 '15 at 13:02
6

Let's say you are pushing your viewcontroller A -> B

In viewcontroller A try adding this code in viewDidLoad method

UIBarButtonItem *btn_Back = [[UIBarButtonItem alloc] initWithTitle:@"Back"
 style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem=btn_Back;

So you will see your "Back" button in viewcontroller B

Ankur
  • 5,086
  • 19
  • 37
  • 62
2

The following should solve your problem

-(void)viewDidAppear:(BOOL)animated
{
    self.navigationController.navigationBar.backItem.title = @"back";
}

You can also use this Objective-C syntax

[[self.navigationController.navigationBar backItem] setTitle:@"back"];
Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
Pratik
  • 2,399
  • 17
  • 36
1

Just do it in this way , one of the easiest way I did find till now :

Create a custom back button and add it into the Navigation Bar left button item property :

- (void)viewDidLoad
{
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"Backnavigation.png"]  ; // Here set the back button image
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 24, 24);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
}


//Handle the Back Button Event
- (void) handleBack:(id)sender
{
    // do your custom handler code here
    [self.navigationController popViewControllerAnimated:YES];
}

Also, doing this way, you don't need to change the title of NavigationBar every-time.

Here is the Back Button for your reference : enter image description here

Ajay Sharma
  • 4,509
  • 3
  • 32
  • 59
0

You can use the below code in the viewWillAppear(animated:Bool) method of the view controller in which you want to set the backButton title of you choice : self.navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "Back Title", style: .plain, target: nil, action: nil)

Najeeb ur Rehman
  • 403
  • 4
  • 11