Can’t seem to get the Back Button to appear in a UINavigationController flow. I just want it to trigger the pop’ing of the current controller automatically to get back to the parent.
I’m getting a little confused in terms of what the minimum necessary is to get an automatic back button (I mean the one with the title of the parent controller and a button that has an arrow pointing back to the left), and what you then need to do to custom things beyond this. I’m just searching for the former at this stage.
@implementation AppointmentListController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Detailed View";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
}
The code to get to this view (i.e. code in parent controller):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
AppointmentListController *appointmentListController = [[AppointmentListController alloc] initWithNibName:@"AppointmentListController" bundle:nil];
[ [self navigationController] pushViewController:appointmentListController animated:YES];
[appointmentListController release];
}
EDIT: PS with the code above I actually don’t see any button at all appear on the left.
EDIT2: David, I tried button the following backbutton code in the RootViewController (and pulled it out of the AppointmentListController), however it still doesn’t show any sort of back button?
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
EDIT3: David – Did work actually with the following code, except that the text was “back” and not the title of the parent controller like I’m after – tried deleting the .title= line but then no button appeared.
// create a custom navigation bar button and set it to always say "Back"
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = @"Back";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
EDIT4 – by bad – I hadn’t set the title of the parent controller so I guess in this case it doesn’t try to but a back button up at all – so adding “self.title = @”Views”;” to the parent controller fixed things (and removing the line where I was manually setting the back button tittle)
EDIT5 – for anyone who was confused like me – so in the end you don’t need any of the backBarButtonItem setting code in the Parent controller at all – just had to make sure the parent controller had a “title” set and then things seem to work automatically – doh
The back button refers not to the top controller, but the second-of-controller stack controller. You put the back button on your detailViewController. You should have put it on your UITableViewController.