I am not able to add UIBarButtonItem in UINavigationController - cocoa-touch

navigator=[[UINavigationController alloc]initWithRootViewController:contacts];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]
initWithTitle:#"Delete"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(makeCall)];
[[self.navigator navigationItem] setLeftBarButtonItem:nextButton];
[nextButton release];
I am not able add UIBarButtonItem in UINavigationController ,please help

Speaking off the cuff here, perhaps instead of
[[self.navigator navigationItem] setLeftBarButtonItem:nextButton];
use
self.navigator.navigationBar.items = [NSArray arrayWithObject:nextButton];
to do what you want.

Related

How to recognize a button press for programmatic UIButton Xcode

Im using this code to create UIBarButtons programmatically on my Navigation Bar, how can I set the Buttons to send IBActions programmatically. I.O.W. how can I programmatically place a view to recognize the UITapGestureRecognizer(set the touchUpInside event) and connect it to an IBAction?
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:153/256.0 green:204/256.0 blue:51/256.0 alpha:1.0];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
self.navigationItem.title = #"Feed";
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
UIBarButtonItem *postItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"myIcon.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *addPlusItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
UIBarButtonItem *optionsItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"myIcon2.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
NSArray *cameraAndPostItem = #[cameraItem, postItem];
NSArray *addPlusAndOptionsItem = #[optionsItem, addPlusItem];
self.navigationItem.leftBarButtonItems = cameraAndPostItem;
self.navigationItem.rightBarButtonItems = addPlusAndOptionsItem;
EDIT: I am trying to perform a segue when the user presses the UIBarButtonItem, which I have implemented programmatically.
set the target and the action of the buttons
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
cameraItem.target = self;
cameraItem.action = #selector(cameraItemButtonInvoked);
//or short
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(cameraItemButtonInvoked)];
...
- (IBAction)cameraItemButtonInvoked {
.... // e.g.
[self performSegueWithIdentifier:#"ShowDetail" sender:sender];
}
You'll be setting the action to UIBarButton like this:
UIBarButtonItem *addPlusItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addSomething:)];
Note the action part, there you are passing the selector to be called. Write that selector in your ViewController like this:
- (void) addSomething:(id)sender{
NSLog(#"I'm adding something..");
}
Do the same for rest of the buttons.

Set navigation bar title to UIBarButtonItem?

here's some code that I am using to add UIBarButtonItems to my navigation bar, how can I change it display custom images, or images uploaded to my Xcode project?
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
NSArray *actionButtonItems = #[shareItem, cameraItem];
self.navigationItem.rightBarButtonItems = actionButtonItems;
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"your_image.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"your_image.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects: shareItem, cameraItem, nil];
Also you can set not only your image, but also your custom UIView with this method:
- (id)initWithCustomView:(UIView *)customView
See more info.
UIBarButtonItemStyleBordered is deprecated in iOS 8. Use UIBarButtonItemStylePlain instead.
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"ic_clear"] style:UIBarButtonItemStylePlain target:self action:nil];
NSArray *actionButtonItems = #[shareItem];
self.navigationItem.leftBarButtonItems = actionButtonItems;

adding menue or buttons for Top Tab bar in objective-C

I want to add 4 buttons or 4 menues in my TOP TAB BAR or top navigation bar, would you please help me that how can I do that should I do that via interface or programmatically and how?
Thanks in advance!
I'm really new to the iOS!
If using iOS 5, you can use the rightBarButtonItems/leftBarButtonItems property of the nav bar.
Just create an array of UIBarButtonItems, and assign that to the appropriate side.
UIBarButtonItem *button1= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(methodOne:)];
UIBarButtonItem *button2= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(methodTwo:)];
UIBarButtonItem *button3= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(methodthree:)];
UIBarButtonItem *button4= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(methodFour:)];
NSArray *buttons = [NSArray arrayWithObjects:button1,button2,button3,button4,nil];
Then, to put these on the left side of the navbar:
self.navigationItem.leftBarButtonItems = buttons;
Or to put them on the right side:
self.navigationItem.rightBarButtonItems = buttons;
You can also use the following to add spaces between your buttons:
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]
NSArray *buttons = [NSArray arrayWithObjects:button1,flexible,button2,flexible,button3,flexible button4,nil];
It's as simple as assigning UIBarButtonItems to your view controller's implied navigationController instance. Forgive the technical jargon, perhaps some code will make up for it:
//left
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]init];
//right
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]init];
Note that this won't work if your root view controller isn't a UINavigationController.

add button on UIToolBar (by code)

how do I add by code on a button to a toolbar that I have the property?
#property (strong, nonatomic) IBOutlet UIToolbar *toolB;
UIBarButtonItem *buttonOne = [[UIBarButtonItem alloc] initWithTitle:#"Button One" style:UIBarButtonItemStyleBordered target:self action:#selector(action)];
UIBarButtonItem *buttonTwo = [[UIBarButtonItem alloc] initWithTitle:#"Button Two" style:UIBarButtonItemStyleBordered target:self action:#selector(action)];
NSArray *buttons = [NSArray arrayWithObjects: buttonOne, buttonTwo, nil];
[toolBar setItems: buttons animated:NO];
Will do the trick if i understand correctly what you are asking. action being the method you want the buttons to call.
This may help you, dont forget to release Buttons.
UIToolbar *actionToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 416, 320, 44)];
UIBarButtonItem *actionButton =
[[[UIBarButtonItem alloc]
initWithTitle:#"No Action"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(noAction:)]
autorelease];
[actionToolbar setItems:[NSArray arrayWithObject:actionButton]];
UIToolbar dont have side buttons, you can use UINavigationBar or follow this link
Aligning UIToolBar items

iPad app navigation bar with multiple buttons on the same side

how can I have a back button and another button both on the left side of the nav bar? since this is an ipad app there's space for multiple buttons. but I can only get it to show one button per side on the navigationitem...
For people who stumbled here and were disappointed by the broken link, check out this article:
http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html
iOS now supports multiple buttons on the same side. From the UINavigationItem docs:
Customizing Views
titleView property
leftBarButtonItems property
leftBarButtonItem property
rightBarButtonItems property
rightBarButtonItem property
– setLeftBarButtonItems:animated:
– setLeftBarButtonItem:animated:
– setRightBarButtonItems:animated:
– setRightBarButtonItem:animated:
So, basically, you would write something like this to add two buttons on the left:
UIBarButtonItem *refreshBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshPlans:)];
UIBarButtonItem *selectYearBtn = [[UIBarButtonItem alloc] initWithTitle:#"Select Year" style:UIBarButtonSystemItemAction target:self action:#selector(selectYear)];
self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWithObjects: refreshBtn, selectYearBtn, nil];
Unfortunately, I haven't seen a way to do this in the Storyboard. Hope this helps.
UIBarButtonItem *btn0 = [[UIBarButtonItem alloc] initWithTitle:#"1" style:UIBarButtonItemStylePlain target:self action:#selector(tapLevel0)];
UIBarButtonItem *btn1 =[[UIBarButtonItem alloc] initWithTitle:#"<2" style:UIBarButtonItemStylePlain target:self action:#selector(tapLevel1:)];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithTitle:#"<3" style:UIBarButtonItemStylePlain target:self action:#selector(tapLevel2:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space.width = 10;
UIBarButtonItem *space2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space2.width = 10;
// UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshItem)];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:btn0, space, btn1, space2, btn2, nil];
found the answer: http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/