I have an application with a navigation bar that navigates from one view controller to the next. When navigating to next view controller on some simulators and devices the back button title is "Back" and on some simulator and devices the back button title is the title of the first view controller.
I would really like to understand why?
This is not an issue of changing the text on the back button. The issue is that on some simulators and devices I see the "back" title and on some simulators and devices I see the title of the previous controller. Tested on over 20 simulators and devices.
Here is the code:
#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface AppDelegate ()
@property (nonatomic, strong) UINavigationController *navigationController;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FirstViewController *firstViewController = [[FirstViewController alloc]
initWithNibName:nil
bundle:nil];
self.navigationController = [[UINavigationController alloc]
initWithRootViewController:firstViewController];
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.rootViewController = self.navigationController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@property (nonatomic, strong) UIButton *btnDisplaySecondViewController;
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)performDisplaySecondViewController:(id)paramSender{
SecondViewController *secondControllrt = [[SecondViewController alloc]
initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:secondControllrt
animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"First Controller";
self.btnDisplaySecondViewController = [UIButton
buttonWithType:UIButtonTypeSystem];
[self.btnDisplaySecondViewController
setTitle:@"Display Seconf View Controller"
forState:UIControlStateNormal];
[self.btnDisplaySecondViewController sizeToFit];
self.btnDisplaySecondViewController.center = self.view.center;
[self.btnDisplaySecondViewController addTarget:self
action:@selector(performDisplaySecondViewController:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.btnDisplaySecondViewController];
UIImageView *imageView =
[[UIImageView alloc]
initWithFrame:CGRectMake(0.0, 0.0, 100.0f, 40.0f)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImage *image = [UIImage imageNamed:@"ios7_0135"];
[imageView setImage:image];
self.navigationItem.titleView = imageView;
}
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Second Controller";
}
When you set the title of the current view controller it will be set automatically in place of Back
in the navigation bar when you move to the next view controller. And if you didn't set the title of the current view controller then iOS
automatically shows the Back
title.
To set the title of the view controller, use the following code:
self.title = @"<title>";
I think you will get this from the explanation.
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