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?
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.
You can do: [self. navigationItem setHidesBackButton:YES]; In your second view controller (the one you want to hide the button in).
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
.
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".
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