How localize the Cancel button in SearchControllerDisplay? - iphone

I know how to add and localize a Done button in NavBar when using the SearchBar.
Like this:
//Add the done button.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Text", #"")
style:UIBarButtonSystemItemDone
target:self action:#selector(doneSearching_Clicked:)] autorelease];
}
Now I want to use the same technic to localize the Cancel button in the SearchControllerDisplay.
But how? I know it can be done but I do not find any solution.

If you want to create a cancel button you just use UIBarButtonSystemItemCancel instead of UIBarButtonSystemItemDone

you could iterate through the subviews of the UISearchBar and "find" a UINavigationButton and then change the title: setTitle:

As far as I know if you set the button type to be a cancel button, iOS will automatically localize it according to the language of the phone.

Related

Making a dynamic NavigationBar

I want a UINavigationBar whose contents change in response to other events in the app. Most immediately, I'd like to have the buttons on the bar loaded dynamically in response to other variables in the program. But in the more general case I'd like to be able to change the contents of the UINavigationBar on the fly, while the program is running.
The hurdle I'm running into is that the UINavigationBar loads its contents when its first displayed and there doesn't seem to be a method to make it alter them after that. What's the best workaround?
This is fairly easy to do. The best way is to pre-load all of the options for the different objects you want in your navBar so that you need only switch them in and out based on the user's input, but you can load them on the fly. When the user does an action which you want to change the navBar, simply add:
self.navigationItem.rightBarButtonItem = nil;
UIBarButtonItem * newButton = [[UIBarButtonItem alloc] initWithTitle:#"What you want to call it" style:UIBarButtonItemStyleBordered target:self action:#selector(whatYouWantTheButtonToCall)];
self.navigationItem.rightBarButtonItem = newButton;
[newButton release];
For other options then a button, you can look here. Hope that helps!
OK, I found one way:
After editing a navigation bar item, you can call
[self loadView]
...to prompt a reload.
That works, but it's a bit blech.

iPhone Add Item/Row Button in Navigation Bar

Is there a standard way to implement a button like the one below in a navigation bar? If not I can, of course, just use an image or maybe a custom view but as it's used in quite a few apps I thought there may be a method I'm missing.
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add:)];
self.navigationItem.rightBarButtonItem=barBtnItem;
[barBtnItem release];
this code use for creating + button.
That's one of the standard UIBarButtonSystemItem system icons (UIBarButtonSystemItemAdd) - take a look at the initWithBarButtonSystemItem:target:action: method in the UIBarButtonItem Class Reference.

How can I add an "add contact" button like the Contacts application?

How can I add to my app that little "plus" button that the Contacts application has in the upper right hand corner of the window?
I would like to be able to hit that button and bring up a screen to add a new entry to the table view.
What you're talking about is a UIBarButtonItem.
Here's an example of how to set one up in code:
UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(showContactView:)];
[self.navigationItem setRightBarButtonItem:addButton];
[addButton release];
When tapped, the button will call the follow selector.
- (void)showContactView:(id)sender {
// Show new contact view.
}
Tom Irving is completely right. It is a UIBarButton and you can configure as he has said. It can be added to a UINavigationBar or UIToolbar as per your requirement.
Hope this helps.
Also dont forget to release this button because some times it causes a crash if you dont release the button instance.

Is it possible to change UIBarButtonItem title and style in runtime?

In one project, I hope to change UIBarButtonItem and style in runtime
editBarItemButton links to a UIBarButtonItem which original status are
style:UIBarButtonItemStyleBordered
title:Edit
if I press the bar item button, it will execute the codes below:
[editBarItemButton setStyle: UIBarButtonItemStyleDone];
[editTarBarItemButton setTitle:#"Done" ];
but neither the style nor title has changed.
Welcome any comment
Thanks
interdev
You cannot change the style of a UIBarButtonItem after it has already been initialized.
Old thread, but I am successful in changing the title for a UIBarButton item in a Navigation controller using the following:
[[[self navigationItem] rightBarButtonItem] setTitle:#"List"];
Just remove the old buttons
Create new buttons with the styles you want
Set the new buttons
What you want to do is something like this (that preserves localization for you app as well):
- (void)setBarButtonTo:(int)type
{
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:type target:self action:nil];
[self.navigationItem setRightBarButtonItem:add];
}
Where type is one of the system supplied, as UIBarButtonSystemItemDone or UIBarButtonSystemItemEdit. Hope this helps. :)
PS: I'm aware it's an old question, but it lacked a proper answer.

How to set the button created in the navigation bar as custom in iphone?

I am new to iphone development.I have created a UIBarButtonItem on the navigation bar.I want to set the proper to custom. There is no property as custom for "style:" attribute.Please help me out.Thanks.
leftbutton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"LEFT.png"] style:UIBarButtonItemStylePlain target:self action:#selector(leftbutton)];
self.navigationItem.leftBarButtonItem = leftbutton;
[self.navigationItem]
[leftbutton release];
If you just want to display a .png file inside the usual button frame from Apple, then your code is ok. If your png file is not displaying, just check if it's valid.
If you want to make your own button, you can use -(id)initWithCustomView:(UIView *)customView
Check this post for an interesting sample code :
Custom Bar Button Sample Code
You can create item with - (id)initWithCustomView:(UIView *)customView method with whatever custom view you want.
UIBarButtonItem has 4 initializers which cover all you will ever need:
-initWithBarButtonSystemItem:target:action: for standard (built-in items);
-initWithTitle:style:target:action: for buttons with title and no image;
-initWithImage:style:target:action: for buttons with image and no title;
-initWithCustomView: for any UIView subclass, Apple's or your own.
The style property makes sense only for standard buttons.