Skip navigation
This discussion is archived

Changing UINavigationBar back button

16516 Views 13 Replies Latest reply: Aug 13, 2008 6:50 AM by Dr. Chuck RSS
mazzilli Level 1 Level 1 (0 points)
Currently Being Moderated
Jul 6, 2008 6:57 PM
Hi all,

I cannot understand why I am unable to modify the title of a back button in a UINavigationController, which has a stack of UIViewControllers.
Here is a code snippet of one of my view controllers. The code is executed and also the navigation bar title is correctly set. Is that a bug in the SDK?


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Breakpoint works here but back button shows default title
self.navigationItem.backBarButtonItem.title = @"All";
}
return self;
}

- (void)loadView {
self.navigationItem.title = @"Title works!";
}
MacBook Pro, Mac OS X (10.5.4), iPhone SDK beta 8
  • sURBHIT Mehrotra Calculating status...
    Currently Being Moderated
    Jul 6, 2008 7:14 PM (in response to mazzilli)
    Put this code in the init , awakefromnib, or view will appear method and it should work.

    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
    temporaryBarButtonItem.title = @"Back To List";
    self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
    [temporaryBarButtonItem release];
    MacBook, Mac OS X (10.5.3)
  • gkstuart Calculating status...
    Currently Being Moderated
    Jul 8, 2008 9:52 PM (in response to mazzilli)
    I think the proper way to create a UIBarButtonItem is with this init method:

    - (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action

    Try that instead of setting the property after an alloc/init. Also try this instead of replacing the backButtonItem:

    navigationItem.hidesBackButton = YES;
    navigationItem.leftBarButtonItem = temporaryItem;


    Hope that works for you!
    George
    MBP Intel 2.4GHz, Mac OS X (10.5.4)
  • Nugae Level 2 Level 2 (240 points)
    Currently Being Moderated
    Jul 8, 2008 11:17 PM (in response to mazzilli)
    I did:

    UIBarButtonItem *temporaryBarButtonItem=[[UIBarButtonItem alloc] init];
    temporaryBarButtonItem.title=@"Dates";
    calendarController.navigationItem.backBarButtonItem = temporaryBarButtonItem;
    [temporaryBarButtonItem release];

    in applicationDidFinishLaunching.
    init won't work, awakeFromNib might work, viewDidLoad would probably work, viewWillAppear may be too late.
    iMac, Mac OS X (10.5)
  • tom145 Calculating status...
    Currently Being Moderated
    Jul 9, 2008 12:00 PM (in response to mazzilli)
    I have the code below. The button shows up and the doneAction routine does get called but it does not go back to the parent controller. Any ideas.

    Tom


    - (id)initWithStyle:(UITableViewStyle)style {
    if (self = [super initWithStyle:style]) {
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"CLOSE" style:UIBarButtonItemStylePlain target:self action:@selector(doneAction:)] autorelease];
    }
    return self;
    }


    -(void) doneAction:(id)sender {
    [[self navigationController] pushViewController:self.parentViewController animated:NO];
    }
    iPhone, Mac OS X (10.5.3)
  • gkstuart Level 2 Level 2 (225 points)
    Currently Being Moderated
    Jul 9, 2008 2:48 PM (in response to tom145)
    You want to popViewController, not pushViewController.

    Cheers,
    George
    MBP Intel 2.4GHz, Mac OS X (10.5.4)
  • pcal43 Calculating status...
    Currently Being Moderated
    Jul 20, 2008 2:46 PM (in response to mazzilli)
    I ran into this as well after upgrading the SDK (setting the button title directly used to just work).

    After some stumbling around, this is what works for me now:


    - (void)loadView {
    //...
    self.navigationItem.backBarButtonItem =
    [[[UIBarButtonItem alloc] initWithTitle:@"Go Back!"
    style:UIBarButtonItemStylePlain target:nil action:nil]
    autorelease];
    //...
    }

    Note that you have to do this in the controller that is the target of the back button (i.e., the controller that will take over when the back button is clicked).
    MBP 2.33, Mac OS X (10.5.4)
  • Dr. Chuck Calculating status...
    Currently Being Moderated
    Aug 12, 2008 7:52 AM (in response to mazzilli)
    *I believe I have found the solution (and what a hack it is, but no new buttons, no explicit popping, and pretty painless) :-)*

    Right before you push the new UIViewController onto the stack, insert the line:

    self.title = @"whatever you want in the back button";

    That will put the modified title in the back button when the new UIViewController is pushed onto the stack. Now, if that's all you do, the original UIViewController will have the new title in the NavBar when the new UIViewController is popped off of the stack. If you don't want that, in the UIViewController in which you did the pushing, you need to override the following method:

    - (void)viewDidAppear:(BOOL)animated
    {
    self.title = @"whatever you want the title to really be";
    }

    That will modify the NavBar title the new UIViewController is popped...

    Cheers, Chuck
    iPhone, Mac OS X (10.4)
  • R.Cerny Level 4 Level 4 (1,170 points)
    Currently Being Moderated
    Aug 12, 2008 8:10 AM (in response to Dr. Chuck)
    Hi folks,
    sorry for my ignorance, but how do you format code blocks in this forum?
    PowerMac G4 1.25DP, Mac OS X (10.4.1)
  • indiekiduk2 Level 1 Level 1 (10 points)
    Currently Being Moderated
    Aug 12, 2008 7:56 PM (in response to Dr. Chuck)
    Dr Chuck, your example isn't correct because I can see the title of the first view chaning to Back before it slides out.

    The real solution is in interface builder for your XIB, change the view mode to the list. Then expand the Navigation Controller and select the Navigation Item. Change the Back button text using the inspector. Done.

    Message was edited by: indiekiduk2
    Mac OS X (10.4.10)
  • Dr. Chuck Level 1 Level 1 (5 points)
    Currently Being Moderated
    Aug 12, 2008 8:11 PM (in response to indiekiduk2)
    Doesn't that hardcode the back button label then? In my case, that's not what I want. I want the label to be dynamic.
    iPhone, Mac OS X (10.4)
  • Dr. Chuck Level 1 Level 1 (5 points)
    Currently Being Moderated
    Aug 13, 2008 6:50 AM (in response to Dr. Chuck)
    Still hacky, but a little neater (and yes, you still see the labels changing):

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

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

    Again, as with one of my previous responses, you can tailor the titles per view controller that you are pushing/popping on/off the stack. In addition, these strings can, of course, be dynamic WITHIN the view controller.

    thanks, Chuck
    iPhone, Mac OS X (10.4)

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.