Reputation: 919
I have referred to quite a few threads (How do I change the title of the "back" button on a Navigation Bar , NavigationController UINavigationBar Back Button and few others) on customizing text in back button to show desired string. However, they do not use storyboard. So many of them are not applicable.
In order to give you a background, I have created my views (view1
and view2
) through storyboard and I would like to use a back button on view2
to navigate to view1
. How can I do this using IB?
Alternatively, I also tried setting self.navigationItem.backBarButtonItem.title
to @"Custom text";
, but this does not work as well.
Can somebody please suggest how can I change the text of Back
button on view2
? Thanks!
Upvotes: 0
Views: 2348
Reputation: 4516
I know it is an old question - but for completeness:
The storyboard way of doing it (personally I like to do as much as possible in Storyboard):
In the view before the one with the back button you want to change (i.e. the destination of the back button you are changing) select the navigation bar and in the Attributes inspector change the value of the "Back Button" field (this is also where you can change the name of the view) - see below.
Upvotes: 3
Reputation: 123
You did not specify if view1 was the previous view of view2 in your question, but if that is what you are trying to do (use a custom title in the back button), then it's really easy to do right within the storyboard.
If you don't see the navigation bar in your storyboard, select your view controller for view1 in the storyboard, and in the Attributes Inspector, make sure the Top Bar has a Navigation Bar selected (instead of Inferred)
Select view1's navigation bar, then in the Attributes Inspector, you have 3 fields: Title, Prompt, and Back Button. If you add text to the Back button field, when you navigate to view2 from view1, the back button will use this text instead of the Title in the back button of view2.
Upvotes: 0
Reputation: 919
I found the solution to my question. The method described below will let you change the text of back button without changing the title of the parent view controller.
Open the .m file of the parent view controller and insert the code.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"saveReturn"])
{
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save & Return" style: UIBarButtonItemStyleBordered target:nil action:nil];
}
`}
Upvotes: 4
Reputation: 80265
Here is the storyboard solution:
In prepareForSegue
set the title of the view controller to the short title that fits onto the back button:
MyViewController *mvc = segue.destinationViewController;
mvc.title = @"Docs";
To change it back, do the reverse in viewWillAppear
:
self.title = @"Important Documents";
Upvotes: 2
Reputation: 158
I am not familiar with Storyboards.
However, usually, the trick is
Right before switching from view1 to view2, set the title of your view1 as you want it on the back button of your view2
[ViewController1 setTitle:@"The text I want in the back button of view2"];
When you switch back to view1 you do not want this title. Hence, in the implementation of the viewcontroller of your view1, you override:
-(void) viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self setTitle:@"The title I actually want in my view1"]; }
This way, when you switch from view1 to view2, you trigger the title you expect to appear on your back button. When you switch back from view2 to view1, you use the fact that view1 "will appear"!
Upvotes: 1