Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 296
VansFannel Avatar asked Feb 14 '12 11:02

VansFannel


People also ask

How to hide back button in Swift iOS?

Basic Swift Code for iOS Apps To hide the back button on navigation bar we'll have to either set the navigation button as nil and then hide it or hide it directly.

How can remove back button in iOS?

You can do: [self. navigationItem setHidesBackButton:YES]; In your second view controller (the one you want to hide the button in).


2 Answers

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.

like image 92
yuji Avatar answered Oct 17 '22 22:10

yuji


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

like image 2
Brody Robertson Avatar answered Oct 17 '22 22:10

Brody Robertson