Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationBar Back Button show "Back" title on some devices or simulators and the previous view controller title on others

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:

App Delegate

#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;
}

First View Controller

#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;
}

Second View Controller

#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";
}
like image 711
Michal Shatz Avatar asked Mar 21 '23 16:03

Michal Shatz


1 Answers

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.

like image 164
Vinay Jain Avatar answered Mar 23 '23 11:03

Vinay Jain