VansFannel
VansFannel

Reputation: 45921

Can't change back button title but I can hide it

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?

Upvotes: 6

Views: 28449

Answers (4)

Brody Robertson
Brody Robertson

Reputation: 8604

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".

Upvotes: 2

rordulu
rordulu

Reputation: 412

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

Upvotes: 0

pvllnspk
pvllnspk

Reputation: 5767

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.

Upvotes: 0

yuji
yuji

Reputation: 16725

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.

Upvotes: 22

Related Questions