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

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.

Related

add multiple UIBarButtonItems to UINavigationItem in Storyboard?

It is easy to add a single UIBarButton item as you can see here in my Storyboard. But I want to add several and it seems I cannot using the Storyboard?
Which is annoying because then I lose the whole nice overview of the Storyboard and it's Segue's.
Is this the case? First project using Storyboards so I figured I might be missing something.
In XCode 4.6 (at least), you can drag a plain view onto the navigation bar (on whichever side you want multiple buttons), then drag a toolbar on top of that. Once you've done that, you can place multiple bar button items on the toolbar, and wire them up to actions / segues, etc, as normal. Make sure you have the view and toolbar both set up for default (transparent) background color.
In Xcode 7 you can finally just drag multiple UIBarButtonItems to the UINavigationItem directly in Interface Builder.
See WWDC 2015 Session 407 video titled:
Implementing UI Designs in Interface Builder
https://developer.apple.com/videos/wwdc/2015/?id=407
Try this
-(void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *editBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editAction)];
UIBarButtonItem *saveBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(saveAction)];
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:editBarButtonItem, saveBarButtonItem, nil];
}
We also need to implement the methods editAction and saveAction.
-(void)editAction
{
NSLog(#"edit button clicked");
}
-(void)saveAction
{
NSLog(#"save button clicked");
}

How localize the Cancel button in SearchControllerDisplay?

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.

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 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.

iPhone app Action Button

I need to add an action button in my iPhone application. When that one is clicked the UIActionSheet with buttons need to be popped up.
Can any one tell me how to add an action button? - means is there any in built action button for iPhone app? or do we need to create a new button with image?
Thanks in advance!
Assuming this is in a view controller, you can do something like:
- (void)viewDidLoad
{
UIBarButtonItem *actionButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(methodThatShowsSheet)];
self.navigationItem.rightBarButtonItem = actionButton;
[actionButton release];
}
(Pardon the weird indentation to make it fit in a reasonable width (I normally just let Xcode wrap-indent everything automatically)).
See Nick Veys' answer for how to implement methodThatShowsSheet.
You need to hook up the action from the button you've decided will present this action sheet to code that shows it.
See the Apple Documentation on UIActionSheet for help on doing that.
I'm not sure I understnd your question but UIKit includes the UIButton class which will automatically send a message (specified by you) to an object (also specified by you). You can create a UIButton i nInterface builder or programatically.
Without more details on whay you actually want to do or why you're finding it difficult,m it's difficult to be more precise.