Question

I have root ViewController and detailed ViewController. When i push to detailedViewController i get leftBarButtonItem with the title from the root one. But i want the title to be just "Back", nothing more. So how to do that?

This doesn't help

self.navigationItem.leftBarButtonItem.title = @"Back";

To create my on type barButtonItem(for example 104 with left arrow) and to set it to leftBarButtonItem is terrible decision.

Is there other way than to change the title of the rootViewController manually before pushing?

Was it helpful?

Solution

From Apple's doc:

backBarButtonItem

The bar button item to use when a back button is needed on the navigation bar.

@property(nonatomic, retain) UIBarButtonItem *backBarButtonItem Discussion

When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. When this property is nil, the navigation item uses the value in its title property to create an appropriate back button. If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.

So, you can create create your barButtonItem (e.g. – initWithTitle:style:target:action:) and assign it to that property.

In addition, if you want to have a custom image for UIBarButtonItem (left or right) I suggest you to create a category extension like the following:

//UIBarButtonItem+Extension.h    
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString*)title target:(id)target action:(SEL)action;

//UIBarButtonItem+Extension.m    
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString*)title target:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.titleLabel.textAlignment = UITextAlignmentCenter;

    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return [barButtonItem autorelease];    
}

and then use it as

UIBarButtonItem* backBarButtonItem = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"YoutImageName"] title:@"YourTitle" target:self action:@selector(doSomething:)];

OTHER TIPS

I finally figured out all the wrinkles in this today, and it's simpler then above.

The child's back button text is based on values set in its parent. This is obvious behaviour, when you think about it: If a view controller can be reached from two parents, the back button's text should depend on which pushed it.

If the text is always the same:

  1. Select the parent view controller's Navigation Item in the editor.
  2. Put the text into the Back Button value.

And like that you're done. When this view controller is pushed aside by a new view controller, that new view controller will get this text as its title.

If the text is dynamic:

  1. Select the parent view controller's Navigation Item in the editor.
  2. Put some text into the Back Button value.
  3. Set the title when it should change in the parent view controller: self.navigationItem.backBarButtonItem.title = dynamicText;

And, again, you're done.

To be clear, you can set this at any time in the parent view controller. It will only be shown when another view controller is pushed.

If you don't put the text in the Back Button in the designer, the process of instantiating the view controller won't create self.navigationItem.backBarButtonItem, and you can't set the title of a nil object. I believe this is where all of the confusion around this stems from.

Of course, you can create this at runtime, but if you're already doing most of your work in the storyboard/nib it's easier to let the decoder do it for you.

If you're more curious about this, I just wrote a blog post on the subject as well. It has some more details.

UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] init];
backButtonItem.title = NSLocalizedString(@"Back", nil);
self.navigationItem.backBarButtonItem = backButtonItem;

It works for me:

[self.navigationItem.leftBarButtonItem setTitle:@"Back"];

(back arrow image and custom text)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top