For one of my view controller (extends UITableViewController), I need to configure its back button text to "Back". But the back button still shows up with parent view controller's title (the default).
- (void)viewDidLoad
{
...
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
}
You need to set the back button on the view controller that pushed the class onto the stack e.g. the controller that created and pushed the class you are showing
The docs for View Controller Programming Guide for iOS suggest that the UINavigationItems are kept in the Navigation Item Stack which provides the title and buttons for the current item on the stack.
Importantly (I added bits in [])
Although most of the navigation bar’s content is obtained from the topmost navigation item [in the navigation stack], a pointer to the back item [of the previous item in the stack] is maintained so that a back button (with the title of the preceding item) can be created.
Looking at the docs under the Configuring the Navigation Item Object section there is a diagram which shows the stack and the backItem pointing to the item below the top item in the stack. In your case the top item in the stack would refer to the UINavigationItem for the class you are showing and the backItem will be a pointer to the class that pushed it.
NB
Look at that section in the docs an image is worth a thousand words
Try this:
UIButton* backButton = [UIButton buttonWithType:101];
[backButton addTarget:self action:@selector(popView:) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:@"Back" forState:UIControlStateNormal];
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backItem;
self.navigationItem.backBarButtonItem.enabled = NO;
self.navigationItem.leftBarButtonItem.enabled = YES;
[backButton release];
You need implement a simple method popView, something like this:
-(IBAction)popView:(id)sender
{
[self.navigationController popViewControllerAnimated:NO];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With