Ohad Regev
Ohad Regev

Reputation: 5711

iOS >> UINavigation Item Back Button Title Doesn't Change

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?

Upvotes: 10

Views: 21764

Answers (7)

smoothumut
smoothumut

Reputation: 3491

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];

Upvotes: 0

Akshit Zaveri
Akshit Zaveri

Reputation: 4244

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];
}

Upvotes: 0

situee
situee

Reputation: 2740

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.

Upvotes: 0

iOSTsunami
iOSTsunami

Reputation: 31

self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"Put Any Title"
                                      style:UIBarButtonItemStyleBordered
                                     target:nil
                                     action:nil];

Upvotes: 3

chris13
chris13

Reputation: 640

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;

Upvotes: 16

Michal Gumny
Michal Gumny

Reputation: 1770

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

Upvotes: 17

johnyu
johnyu

Reputation: 2151

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.

Upvotes: 14

Related Questions