add multiple UIBarButtonItems to UINavigationItem in Storyboard? - iphone

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");
}

Related

initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl not working

I am creating a barbutton item and adding it to my navigation controller toolbar
UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl target:self action:#selector(pageCurlAction:)] autorelease];
And the barbutton item is rightly loading with the toolbar in my viewcontroller.Now, I push a new viewcontroller into my view and when I pop the viewcontroller, my barbutton item with the page curl shrinks and and the page curl image is missing on the barbutton.
I tried changing the
initWithBarButtonSystemItem
with
UIBarButtonSystemItemPlay, UIBarButtonSystemItemPause, UIBarButtonSystemItemFastForward
etc. But, it works fine with them. Its just the page curl icon thats not being loaded. Any reasons for this?
From the iOS Human Interface Guidelines:
In addition, you can use the system-provided page curl button in a toolbar (for more information, see the documentation for UIBarButtonSystemItemPageCurl in UIBarButtonItem Class Reference). The page curl button is not available for use in a navigation bar.
(Emphasis added.)
Even if you use a UIToolbar, this problem can appear. It turns out the order in which you do styling and adding of toolbar items matters.
In my case I had to change from:
[self.toolbar setItems:items];
self.toolbar.tintColor = [UIColor grayColor];
to:
self.toolbar.tintColor = [UIColor grayColor];
[self.toolbar setItems:items];
in order for UIBarButtonSystemItemPageCurl to start working on iOS 4.3

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.

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.