add button on UIToolBar (by code) - objective-c

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

Related

barButtonItem in UIToolBar of NavigationController not working in ios7

I am trying to add BarButton in UIToolBar of NavigationController. I found on below code on stack over flow. But it is not working in ios7. If anything changed why it shows nothing on tool bar.
[self.navigationController setToolbarHidden:NO];
UIBarButtonItem *buttonItem = [[ UIBarButtonItem alloc ] initWithTitle: #"Select All"
style: UIBarButtonItemStyleBordered
target: self
action: #selector(selectAll:) ];
UIBarButtonItem *buttonNext = [[UIBarButtonItem alloc]initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(goNext:)];
self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ];
//allocate the tool bar
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 416, 320, 44)];
// create bar btns
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:#selector(Back)];
//set spacing
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:#selector(Forward)];
// add btns to the bar
[toolBar setItems:[NSMutableArray arrayWithObjects:back,flexibleSpace,forward, nil]];
// adds the toobar to the view
[self.view addSubview:toolBar];
I found the problem. Above code is correct. The reason why there is no button showing in the tool bar . i also have this function in my class. this is a empty function. so it overwrite the default self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ];
After Removing the below code. Its working Perfect Now.
- (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated
{
}

UIPickerView toolbar buttons not working

I'm trying to add a toolbar to a UIPicker.
I've seen that the right way to go is this way:
UIToolbar* toolbarWeight= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *barButtonDone1 = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(gotoNextField:)];
[toolbarWeight setItems:[NSArray arrayWithObject:barButtonNext1] animated:YES];
weightPickerView = [[UIPickerView alloc]init];
[weightPickerView addSubview:toolbarWeight];
While:
-(IBAction)gotoNextField:(id)sender{
NSLog(#"Works");
}
The picker works just fine but the button doesn't work.
After doing some research, I've tried this approach:
(I suspected that the problem was related to the UIBarButtonItem action)
UIButton* myBtn = [[UIButton alloc]init];
myBtn.frame = CGRectMake(0,0,50,24);
[myBtn addTarget:self action:#selector(gotoNextField:) forControlEvents:UIControlEventTouchUpInside];
[myBtn setTitle:#"Next!" forState:UIControlStateNormal];
[myBtn setBackgroundColor:[UIColor orangeColor]];
UIBarButtonItem *barButtonNext1 = [[UIBarButtonItem alloc] initWithCustomView:myBtn];
[toolbarWeight setItems:[NSArray arrayWithObject:barButtonNext1] animated:YES];
Doesn't work either. The button appears but doesn't get selected/respond to touchUpInside.
Here an example for adding a UIPickerView including a Toolbar - works with iOS 7
Make sure to implement the interfaces UIPickerViewDataSource, UIPickerViewDelegate and also implement the #selector methods
pickerView = [[UIPickerView alloc] init];
[pickerView setDataSource: self];
[pickerView setDelegate: self];
pickerView.showsSelectionIndicator = YES;
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(itemWasSelected:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(logoutData)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:#"Clear" style:UIBarButtonItemStyleBordered target:self action:#selector(clearData)];
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, clearButton, flexible, doneButton, nil]];
pickerParentView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, 320, 260)];
[pickerParentView addSubview:pickerView];
[pickerParentView addSubview:toolBar];
[self.view addSubview:pickerParentView];
As adding a toolbar to the pickerView, the toolbar is added behind the picker preventing the buttons from responding to touch events.
Encapsulating both the picker and the toolbar in a view, and making it an inputView (to make both the picker and the toolbar popup together) provides the desired solution.
weightPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, 320, 216)];
weightPickerView.tag = 13;
UIView* genericWeightView = [[UIView alloc]initWithFrame:CGRectMake(0, 219, 320, 266)];
[genericWeightView addSubview:toolbarWeight];
[genericWeightView addSubview:weightPickerView];
and then:
[weight_txtField setInputView:genericWeightView];

How to add UIBarButtonItem in UIToolBar in code

I have standart UIBarButtonItem
UIBarButtonItem *share = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(share:)];
How to add her to UIToolBar? I've tried
self.toolbarItems = [NSArray arrayWithObject:share];
But it doesn't work. Need your help.
Can you be more specific than "it doesn't work"?
If you're trying to add an item to a toolbar that already has items, you'll need to modify the array of items:
NSMutableArray *newItems = [self.toolbarItems mutableCopy];
[newItems addObject:share];
self.toolbarItems = newItems;
Make sure you have make a toolbar either an IBOutlet or added toolbar programatically
IBOutlet UIToolbar *toolBar;
UIBarButtonItem *infoButtonItem=[[UIBarButtonItem alloc]initWithTitle:#"info" style:UIBarButtonItemStyleBordered target:self action:#selector(infoButtonClicked)];
toolBar.items = [NSArray arrayWithObjects:infoButtonItem, nil];
Make sure the toolbar isn't hidden; you could try adding the following to your view controller's viewWillAppear:animated: method:
[self.navigationController setToolbarHidden:NO animated:YES];
[toolbar setItems:[NSArray arrayWithObject:share] animated:YES];

I am not able to add UIBarButtonItem in UINavigationController

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.

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/