0Day Forums
How do I change the title of the "back" button on a Navigation Bar - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Objective-C (https://0day.red/Forum-Objective-C)
+--- Thread: How do I change the title of the "back" button on a Navigation Bar (/Thread-How-do-I-change-the-title-of-the-quot-back-quot-button-on-a-Navigation-Bar)

Pages: 1 2 3


How do I change the title of the "back" button on a Navigation Bar - suspensoid319 - 07-21-2023

Currently the left bar button default value is the title of the view that loaded the current one, in other words the view to be shown when the button is pressed (back button).

I want to change the text shown on the button to something else.

I tried putting the following line of code in the view controller's viewDidLoad method but it doesn't seem to work.

self.navigationItem.leftBarButtonItem.title = @"Log Out";

What should I do?

Thanks.



RE: How do I change the title of the "back" button on a Navigation Bar - imido760551 - 07-21-2023

Here's another way to do it.

In your parent view controller, implement the following method:

- (void) setBackBarButtonItemTitle:(NSString *)newTitle {
self.navigationItem.backBarButtonItem.title = newTitle;
}

In your child view controller, when you want to change the title, this will work:

NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBackBarButtonItemTitle:@"New Title"];

I was never able to get the `parentViewController` property to work:

[(ParentViewController *)(self.navigationController.parentViewController) setBackBarButtonItemTitle:@"New Title"];

I don't know if that's a bug or I'm not using it properly. But grabbing the second-to-last view controller in the `viewControllers` array points to the parent view controller, and I can call parent methods correctly with that reference.


RE: How do I change the title of the "back" button on a Navigation Bar - maiernf - 07-21-2023

Maybe I'm being over simplistic but From Apple's documentation the wording is:

> If a **custom** bar button item is not specified by either of the view controllers, a **default back button** is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack.

The solution marked correct above sets a default button item from the parent controller. It's the right answer, but I'm solving the issue by changing `self.title` property of the UIViewController right before pushing the new controller onto the NavigationController stack.

This automatically updates the back button's title on the next controller, and as long as you set `self.title` back to what it should be in `viewWillAppear` I can't see this method causing too many problems.


RE: How do I change the title of the "back" button on a Navigation Bar - gnnikepgdpruyk - 07-21-2023

self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Log out"
style:UIBarButtonItemStyleDone
target:nil
action:nil] autorelease];

you can put it whereever you like in the code in the parrent controller, which allowes you to have differenct backbuttons for different child views.


RE: How do I change the title of the "back" button on a Navigation Bar - sexualization851525 - 07-21-2023

Ok, here is the way. If you have a view controller "first" and you navigate another view controller "second" by pushing a button or etc. you need to do some work.
First you need to create a BarButtonItem in "second" view controller's ViewDidLoad method like this;

UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(OnClick_btnBack:)];
self.navigationItem.leftBarButtonItem = btnBack;
[btnBack release];

After you do that, you need to write to code for "btnBack" action in the same .m file like this;

-(IBAction)OnClick_btnBack:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
//[self.navigationController pushViewController:self.navigationController.parentViewController animated:YES];
}

That's all.




RE: How do I change the title of the "back" button on a Navigation Bar - herthagoygmcf - 07-21-2023

I've found, that the easiest way to change the name of the back button is to set the view controllers title to the title of the back button, and then replacing the titleView in the view controllers navigation item to a custom label with it's real name.

Like this:

CustomViewController.m

@implementation CustomViewController

- (NSString*)title {
return @"Back Button Title";
}

- (void)viewDidLoad {
[super viewDidLoad];
UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
customTitleView.text = @"Navigation Bar Title";
customTitleView.font = [UIFont boldSystemFontOfSize:20];
customTitleView.backgroundColor = [UIColor clearColor];
customTitleView.textColor = [UIColor whiteColor];
customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
customTitleView.shadowOffset = CGSizeMake(0, -1);

[customTitleView sizeToFit];

self.navigationItem.titleView = [customTitleView autorelease];
}

@end

This will make your title in UINavigationBar look as if it was native. Giving the view controller the ability to have seperated title and back button title.

In the case of view controller A and B, A is responsible for telling how it's back button should look, while B is displayed.

EDIT: This also maintains the back button native look (The left arrowed bar button item).



RE: How do I change the title of the "back" button on a Navigation Bar - pieterwc - 07-21-2023

ok. I personally hated all of these options. Therefore I came up with my own.

Based on the information I have seen. It appears that the Previous view controller is in control of its own "Back" button that will be presented on the pushed view controller.

I have created a Lazy Load method for the navigationItem on the controller that wants the changed Back Button.

Mine is an Invite Buyer Controller

Invite Buyer is the text that is set by default.

but the back button needed to be Invite

Here is the code that I used to create the back button.

I placed this code in the top of the Controller's Implementatio (.m) file and it overrode the super's method automatically.

- (UINavigationItem *)navigationItem{
UINavigationItem *item = [super navigationItem];
if (item != nil && item.backBarButtonItem == nil)
{
item.backBarButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
item.backBarButtonItem.title = @"Invite";
}

return item;
}

I feel this is a much more elegant way to accomplish this.

I place this code in one place, and it automatically gets populated when needed.

No need to call the code before each push request.

Hope this helps


RE: How do I change the title of the "back" button on a Navigation Bar - monterrey604 - 07-21-2023

Most of solutions kills the original style of BackButton (The left arrowed bar button) while adding a usual button with desired title.<br>
So to keep the original style there are 2 ways:<br>
1st: To use undocumented button style (110 or something like that) which I prefer not to do. But if you want you could find how to do it here, on stackoverflow.<br>
2nd: To use I the Trenskow's idea. I liked it and I use it a bit changed.<br>
Instead of overriding - (NSString*)title I've decided to keep the original title in the following way (which allows me to use nib's titles as well as given title at push state btw).

- (void)viewDidLoad {
[super viewDidLoad];
static NSString * backButtonTitle=@"Back"; //or whatever u want

if (![self.title isEqualToString:backButtonTitle]){

UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
customTitleView.text = self.title; // original title
customTitleView.font = [UIFont boldSystemFontOfSize:20];
customTitleView.backgroundColor = [UIColor clearColor];
customTitleView.textColor = [UIColor whiteColor];
customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
customTitleView.shadowOffset = CGSizeMake(0, -1);

[customTitleView sizeToFit];

self.navigationItem.titleView = [customTitleView autorelease];
self.title = backButtonTitle;
}
}

This solution works good and it looks native. Also if use it in the viewDidLoad method it prevents execution more then 1 time.<br>
Also I've tried a Jessedc's solution but it looks bad. It causes visible to user title bar change on the fly from original to BackButton's desired and back.



RE: How do I change the title of the "back" button on a Navigation Bar - ruta718750 - 07-21-2023

UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(OnClick_btnBack:)];
self.navigationItem.leftBarButtonItem = btnBack;
[btnBack release];




RE: How do I change the title of the "back" button on a Navigation Bar - incinerator162 - 07-21-2023

For those using storyboards just select the parent (not the one that is holding target view) view controller frame (be sure you click right on the Navigation bar, then open attributes inspector, where you'll find three form inputs. The third one "back button" is that we are looking for.