How to change the text on UINavigationBar backButton item? - iphone

I know the label on back button item is always the last view clicked, but how should I go if I want to have "back" text on it?

self.navigationItem.backBarButtonItem =[[[UIBarButtonItem alloc]
initWithTitle:#"Your Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil] autorelease];

This issue bugs me as well. My hack is to change the title of the view controller in -viewWillAppear: and -viewWillDisappear:, where the latter changes the title to what will appear on the "back" button and the former changes it back to what I want the title of that screen to be. For example,
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
myViewController.navigationItem.title = #"My Title";
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
myViewController.navigationItem.title = #"Back";
}

Related

DetailView Page inheriting code from RowView

I have a detailview page, and on top of it the title lable appears to be the title of the row i navigated from. I need to just not display anything in the navigationbar but the title appears and i dont know where to change it.
I also have a backbutton however if i use navigation.backbarbuttonItem then the title of the previous page appears on the button. I want the button to just have back on it. I created a button using leftbarbuttonItem but then the button cannot be styled to look like a backbutton (with one side pointed). It appears as just a rectangular button which i dont like.
Could anyone know how to adjust this?
For number 1, you can just set the viewController title on viewWillAppear or viewDidAppear, lets say
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.title = #"";
}
For number 2, you can use this code to have the "Back" pointy button.
UIViewController *viewController = [UIViewController alloc] init];
// This code
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[self.navigationController pushViewController:viewController animated:YES];

how to change back button title of navigation in iphone

How to not show Back Bar Button of navigation controller. When I am trying to write my title with " " then this is showing default title name (Root).
How to change it ?
if you don't want to see the back button, use below
self.navigationItem.backBarButtonItem = nil ;
if you want to see your text instead of "Back" button. you could also define your action:.
self.navigationItem.backBarButtonItem =
[[[UIBarButtonItem alloc] initWithTitle:#"MyOwnBackTitle"
style: UIBarButtonItemStyleBordered
target:self
action:#selector(backClicked:] autorelease];
Use below code to change the bar text,
UINavigationBar *bar = self.navigationController.navigationBar ;
bar.topItem.title = #"MY Title";
Use this in the viewDidLoad method
[self.navigationItem setHidesBackButton:YES animated:YES];
self.navigationController.navigationBar.topItem.title=#"Your Title";

navigationItem.backBarButtonItem not working? Why is the previous menu still showing as the button?

Trying to customize my back button in a drilldown navigation controller.
On my one view controller I have an Add button where the code programatically generates a new UIViewController:
- (void)add:(id)sender
{
MyAddViewController *addController = [[MyAddViewController alloc] initWithNibName:#"MyAddViewController" bundle:nil];
[self.navigationController pushViewController:addController animated:YES];
[addController release];
}
This works and when I click the add button it drills down into the new view. Inside the viewDidLoad method of MyAddViewController.m I have:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
But this isn't working. The back button in the navigation controller remains the title of the previous view's controller on the stack. It seems that line does nothing. Did I miss something?
Thanks
self.navigationItem.backBarButtonItem is for the back button that appears on the view pushed by the view controller. So you need to move that line to the previous view controller.
This will only work on each child after the viewController that has self.navigationItem.backBarButtonItem.
You're confusing the backBarButtonItem and the leftBarButtonItem. From the UINavigationItem docs on backBarButtonItem:
When this item is the back item of the
navigation bar—when it is the next
item below the top item—it may be
represented as a back button on the
navigation bar. Use this property to
specify the back button. The target
and action of the back bar button item
you set should be nil. The default
value is a bar button item displaying
the navigation item’s title.
So, if you were to change:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
To:
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
I believe you would get the desired effect.
You can't replace the backBarButtonItem, but you can use the leftBarButtonItem to override it. But to get the new button to perform operate the same as the back button, you do need to set the target and action of the new button something like:
- (void)dismissMyView {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:#"Quit" style: UIBarButtonItemStyleBordered
target:self action:#selector(dismissMyView)];
}
If ViewController A push ViewController B meanwhile we want to set the back bar button tittle, we should set "self.navigationItem.backBarButtonItem = ..".if it was set in ViewController B, it will not work as we want.

screen naviagation iphone, xcode?

i'm having 3 screens
set title to screen1 using
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title =#"Products";
}
when navigation to next screen (screen2) second screen having the title "Categories"
but there is a button on the top name "Products" to Screen1
i want to change the button name as "Back"
Please tell me how?
triedself.navigationItem.leftBarButtonItem .title = #"Back";
See these two:
How to set the text of a back button on a UINavigationBar?
How do I change the title of the "back" button on a Navigation Bar
Solves your problem.
UIBarButtonItem *item=self.navigationItem.leftBarButtonItem;
item=[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem=item;
[item release];
Put this code in your first scree viewDidLoad method as it is It really works.
You have to do something like the follows to set the text of the back bar button to something other than what the previous view was named:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = #"Your Custom Title";
self.navigationItem.backBarButtonItem = barButton;
You CANNOT do the following:
self.navigationItem.backBarButtonItem.title = #"Your Title";
This sets the previous view on the stack to "Your Title" and not was it was before.

How do you change color/image on the default backBarButtonItem?

I need to change color on the default "self.navigationItem.backBarButtonItem". To accomplish this I´ve created an custom Button class and implemented it like this:
- (void)viewDidLoad {
[super viewDidLoad];
BackButton *blueSaveButton = [[BackButton alloc] init];
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:blueSaveButton];
temporaryBarButtonItem.title = #"Tillbaka";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
[blueSaveButton release];
[self gotoLocation];
}
But this has no effect at all on the button.
So how do you manage to do this without "breaking" the default/inherited behavior of the navigationbar?
EDIT: The reason is that the button needs to be corporate branded, so default styles will not do.
BR
If you want the button to be blue (like a Done button) and have the title "Save" you can use one of the built in bar button item types:
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(myAction:)] autorelease];
Ok found the solution (not so good but it works) do this:
1: Implement the button from this tutorial: www.switchonthecode.com
This will override the default behavior of navigationbar.
2: Implement this in every view that needs the custom button:
osmorphis.blogspot.com
BR