6

I'm developing an iPhone application with latest SDK and XCode 4.2

I use a UINavigationController, and to show a new ViewController I do this on AppDelegate.m:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
        }
        else
        {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
        }
        self.viewController.title = NSLocalizedString(@"MAIN", nil);

        navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
        navController.navigationBar.tintColor = [UIColor darkGrayColor];

        self.window.rootViewController = navController;
        [self.window makeKeyAndVisible];
        return YES;
    }

    - (void) openDocumentation
    {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
        {
            docViewController = [[DocumentationViewController alloc] initWithNibName:@"DocumentationViewController_iPhone" bundle:nil];
        }
        else
        {
            docViewController = [[DocumentationViewController alloc] initWithNibName:@"DocumentationViewControllerr_iPad" bundle:nil];
        }
        docViewController.title = NSLocalizedString(@"DOCUMENTATION", nil);
self.viewController.navigationItem.backBarButtonItem.title = @"Back";
        [navController pushViewController:docViewController animated:YES];
    }

On DocumentationViewController.m I do this:

- (void) viewWillAppear:(BOOL)animated
{
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                   style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backButton;
}

But it doesn't change back button title.

I move that code to viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                   style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backButton;
}

- (void) viewWillAppear:(BOOL)animated
{

}

But still doesn't work.

And this doesn't work either:

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

But if I do this, to test if I can't access back button:

- (void) viewWillAppear:(BOOL)animated
{
    self.navigationItem.hidesBackButton = YES;
}

It works!! And hides back button.

What am I doing wrong?

VansFannel
  • 45,055
  • 107
  • 359
  • 626

4 Answers4

22

The back button title shown by a UINavigationViewController doesn't depend on the navigationItem.backBarButtonItem.title of the topmost view controller in the stack. It depends on the second topmost view controller's value of navigationItem.backBarButtonItem.title.

So you need to set navigationItem.backBarButtonItem.title for whatever view controller is showing when you push your DocumentViewController.

yuji
  • 16,695
  • 4
  • 63
  • 64
  • Ok, and now: How can I change it? – VansFannel Feb 14 '12 at 11:31
  • I'm sorry but I don't understand you. I have added this line of code at the end of AppDelegate openDocumentation method: `navController.navigationItem.backBarButtonItem.title = @"Back";`. But, it still doesn't show Back text. – VansFannel Feb 14 '12 at 11:36
  • AppDelegate push 'ViewController'. On 'ViewController' I click on a button that calls AppDelegate openDocument method. This method push 'DocumentViewController'. – VansFannel Feb 14 '12 at 11:43
  • So you need to set the `navigationItem.backBarButtonitem.title` for 'ViewController' – yuji Feb 14 '12 at 11:45
  • Sorry, but it still doesn't work. I have updated my question with new code but your answer doesn't work. – VansFannel Feb 15 '12 at 20:32
  • When do you push ViewController onto the navigation view controller? I don't see you doing that in your code. Like I've said before, the navigation bar's back button title is based on the `navigationItem.backBarButtonItem.title` of the **second from top** view controller in the navigation stack. – yuji Feb 15 '12 at 20:39
  • I pust viewController in AppDelegate didFinishLaunchingWithOptions. Then, when user clicks on document button in viewController I call AppDelegate openDocumentation. In this method I set `self.viewController.navigationItem.backBarButtonItem.title = @"Back";` – VansFannel Feb 15 '12 at 20:42
  • Try `self.viewController.navigationItem.title = @"Back";` instead – yuji Feb 15 '12 at 20:44
  • Seriously? No. You have `self.viewController.navigationItem.backBarButtonItem.title = @"Back";` I said `self.viewController.navigationItem.title = @"Back";` – yuji Feb 15 '12 at 20:48
  • On DocumentationViewController viewWillAppear: backBarButtonItem is nil. I do `self.navigationItem.backBarButtonItem = backButton;` and then I don't see back. Your previous comment changes viewController title, and I don't want to do that. – VansFannel Feb 15 '12 at 21:14
  • Fine, then set `self.viewController.navigationItem.backBarButtonItem` = [[UIBarButtonItem alloc] initWithTitle:@"WhateverYouWantYourTitleTobe" style:UIBarButtonItemStylePlain target:nil action:nil]; – yuji Feb 15 '12 at 21:20
  • Yuji's got it correct here. A [link to a related answer](http://stackoverflow.com/questions/1441699/uinavigationcontroller-back-button-custom-text/1441718#1441718) may help — the solution you've linked to is… to quote the article, crappy. Set the `self.navigationItem.backBarButtonItem` property (a `UIBarButtonItem`) on the controller doing the stack-pushing. – Ben Kreeger Sep 18 '12 at 18:44
  • this may help as well. – minhazur Dec 30 '15 at 09:54
2

When you set the backBarButtonItem title of a UIViewController, you are actually setting the title for the back button if this UIViewController is the second controller in the UINavigationController stack.

self.navigationItem.backBarButtonItem.title = @"Custom Title";

So, if the above view controller is already in the nav controller and subsequently pushes a child view controller into the nav controller, the back button title will be "Custom Title".

Brody Robertson
  • 8,506
  • 2
  • 47
  • 42
0

Just add in the Storyboard or Nib file your custom button (this will replace the navigation button) and add a back action for it:

- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

For me it allows to customize my back button.

pvllnspk
  • 5,667
  • 12
  • 59
  • 97
0

Actually you can do this with just one trick:

Override UINavigationBar class and add this line of code:

- (void)layoutSubviews{
    self.backItem.title = @"";
    [super layoutSubviews];
}

Then initialize your UINavigationController with this custom UINavigationBar class.. etc.

UINavigationController * navController = [[UINavigationController alloc] initWithNavigationBarClass:[CBCNavigationBar class] toolbarClass:nil]

Hope this helps

VansFannel
  • 45,055
  • 107
  • 359
  • 626
rordulu
  • 412
  • 4
  • 12