10

I'm trying to set the BACK button for a pushed VC set within a UINavigationController stack. I use the following code, and it's not working - I still get the previous VC name appearing as the back button title.

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    self.title = @"VC Title";

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

    self.navigationItem.backBarButtonItem = myBackButton;

}

Anyone?

Ohad Regev
  • 5,641
  • 12
  • 60
  • 84

7 Answers7

17

in parent view controller:

- (void)viewWillDisappear:(BOOL)animated
{
    self.title = @"Back";
}

- (void)viewWillAppear:(BOOL)animated
{
    self.title = @"Title of your navigation bar";
}

Will do the trick

Michal Gumny
  • 1,770
  • 1
  • 16
  • 24
16

Try setting the title in the parent view controller's viewDidLoad

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(popView)];

self.navigationItem.leftBarButtonItem = customBarItem;
Govind
  • 2,337
  • 33
  • 43
chris13
  • 640
  • 5
  • 14
  • 3
    Thanks chris13. That did it. Weird though that self.navigationItem relates to the next VC and not to the currently active one... Any idea why or maybe you have a link to a good explanation about it? – Ohad Regev Aug 26 '13 at 13:49
  • 1
    I also think that it's weird but the Apple Docs make it a bit clearer.. Please accept the answer if it helped you ;) – chris13 Aug 26 '13 at 13:56
  • 1
    Simply use Storyboard - UIController - Naviation Item and set "Back Button" in the Attributes Insprector for THIS view. The NEXT view will use this as Back Button text. – David Sep 08 '14 at 23:31
14

From Apple's documentation:

The bar button item on the left side of the navigation bar allows for navigation back to the previous view controller on the navigation stack. The navigation controller updates the left side of the navigation bar as follows:

If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.

If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has a valid item in its backBarButtonItem property, the navigation bar displays that item.

If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack. (If there is only one view controller on the navigation stack, no back button is displayed.)

Hope this helps.

johnyu
  • 2,152
  • 1
  • 15
  • 33
  • Thanks johnyu. But if I take the first option of setting a custom barButton as the navigationItem.leftBarButtonItem it doesn't have the default back button "arrow" shape look. Any idea how to solve that? – Ohad Regev Aug 26 '13 at 14:03
  • +1 Here is the link : Updating the Navigation Bar https://developer.apple.com/library/ios/documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006934-CH3-SW25 – situee Aug 14 '14 at 09:23
  • Thanks! Finally I managed to give the behavior I wanted to my back button. – Felix D. Nov 25 '14 at 04:41
3
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"Put Any Title"
                                      style:UIBarButtonItemStyleBordered
                                     target:nil
                                     action:nil];
Pranav Singh
  • 17,079
  • 30
  • 77
  • 104
iOSTsunami
  • 31
  • 4
0

Set a backBarButtonItem to the navigationItem of the previous viewController. Check this answer https://stackoverflow.com/a/25680043/111277. Check my blog post iOS Set Navigation Bar Back Button Title for detail analysis.

Community
  • 1
  • 1
situee
  • 2,710
  • 2
  • 22
  • 27
0

One more solution, which is very quick.

Override this method in your Base view controller and you will have back button on every pushed view controller. (Just do not add [super setTitle:title])

- (void)setTitle:(NSString *)title
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    [lbl setText:title];
    [lbl setTextColor:[UIColor whiteColor]];
    [self.navigationItem setTitleView:lbl];
}
Akshit Zaveri
  • 4,166
  • 6
  • 30
  • 59
0

for storyboard solution;

click on the previous view controller's navigation item. then click on the attributes inspector on the right pane, then write " " or anything else on the backbutton area. this will tell view controller what to show when in the next(child) view controller. hope it helps

for code solution;

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:@"back off" style:UIBarButtonItemStyleBordered target:self action:@selector(popView)];
[[self navigationItem] setBackBarButtonItem:customBarItem];
smoothumut
  • 3,423
  • 1
  • 25
  • 35