4

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";
}
Michal Shatz
  • 1,416
  • 2
  • 20
  • 31
  • possible duplicate of [Navigation Controller Back Button Doesn't Show Text](http://stackoverflow.com/questions/21356567/navigation-controller-back-button-doesnt-show-text) – Larme Feb 05 '14 at 14:10

5 Answers5

4

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.

Vinay Jain
  • 2,644
  • 3
  • 26
  • 44
  • No. I set the title of the view controller and tested on about 20 simulators. Some showed "back" and some the title of the previous view. – Michal Shatz Feb 05 '14 at 14:27
  • Ok, i read your code and its fine. Can you please categorise the iOS simulators like on which version you are getting Back and on which you are getting Title. – Vinay Jain Feb 06 '14 at 04:41
2

This is an old question, but I found the same just a second ago. For me, it is a matter of display space. On my iPhone 5, in portrait, I get the short "Back" text, if I rotate the view, it will show the previous' controllers title.

thst
  • 4,592
  • 1
  • 26
  • 40
1

There is a different behavior in iOS6 and iOS7. In iOS7 the title of your first controller will be changed to "Back" (on the left navigationItem in the second VC) if its length exceed 11 characters. In iOS6 the title of the navBar gets truncated and/or the title of the back item.

ClemensL
  • 404
  • 2
  • 10
0

Hi you can check this one Please upward if your problem has been solve so that the other can be benifited from it.

When my app returns from gallery, it has the navbar's standard colors

Community
  • 1
  • 1
Harunmughal
  • 367
  • 1
  • 4
0

Additionally, if you want to change the name of your back button to be different than that of the previous view controller's title, you can do so by creating a custom back button item on the navigation item of the controller being pushed onto the navigation controller stack:

custom UIBarButtonItem for back button

Apple's reference here for backBarButtonItem

Community
  • 1
  • 1
cohen72
  • 2,830
  • 29
  • 44