Setting the back button title of a UINavigationItem

The following code will set the back button title of the current UINavigationItem:

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

Updated to fix memory leak. (Thanks Sam!)

The target: and action: parameters are definitely ignored (replaced by the default back button behavior) and the style also seems to be ignored because the back button retains its pointed left side.

This next block of code looks like it should do the same thing but does not work:

self.navigationItem.backBarButtonItem.title = [NSString stringWithString:@"Back"];

MAJOR CAVEAT: As pointed out in this thread, "the back button is 'owned' by the previous view on the stack." In other words, the setBackBarButtonItem method should be called on the view that the user is navigating away from, rather than the view above which this button is actually displayed.

UINavigationItem on Apple Developer Docs

5 Comments on Setting the back button title of a UINavigationItem

You can track this conversation through its atom feed.

  1. shoaib on April 2nd, 2009 at 9:52 pm

    Hi,

    By adding the above code, Back button is enabled but it shows memory leakage. Is there any way to avoid memory leakage?

  2. George on April 15th, 2009 at 4:48 am

    Thank you for this! I was banging my head against the wall on this one - the Apple developer site only hints at what needs to be done.

  3. George on April 15th, 2009 at 4:56 am

    PS to Shoaib, try looking at where you have placed the back button code. As Adam mentioned, it should go in the view that the user is navigating away from, rather than in the view that the user is navigating to. It seems to work wherever you put the code, but I think it should go right before your:

    [self.navigationController pushViewController:newViewName animated:YES];

    statement.

  4. Sam Soffes on July 13th, 2009 at 2:52 pm

    That would create a leak. Do this instead:


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

  5. Duncan Champney on January 8th, 2010 at 9:36 pm

    @shoaib,

    shoaib,

    The memory leakage is because the UIBarButtonItem is being created with a retain count of 1. It should be autoreleased before adding it to the navigation item, like this: (split into 2 lines for clarity)


    UIBarButtonItem* newBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Map" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
    [theMapViewController.navigationItem setBackBarButtonItem:newBarButtonItem];

Leave a Comment

(required) (required, but not published) (optional)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>