UIToolbar creation - ios

i have created a toolbar as below shown code and now i dont knw how to add the items to it.
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 418, 350, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[[UIBarButtonItem alloc] init] autorelease]];
[toolbar setItems:items animated:YES];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
}

Try this code
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"Item" style:UIBarButtonItemStyleBordered target:self action:#selector(action:)];
NSArray *items = [NSArray arrayWithObjects: customItem, nil];
[toolbar setItems:items animated:NO];
[customItem release];
You can also refer to this link:
How to create this toolbar programmatically
Please do google search and Stack Overflow search before posting.

use
[self setToolbarItems: buttons animated:NO];
as u want to set toolbaritems and uiviewcontroller has toolbar itself.

Related

Flexible bar button item now displayed on UIToolbar

I know it may seem very simple and has been asked many times but i can't get it to work .I want to add two buttons to my toolbar. One on the right side and the other one on the left. Here is the code but the flexible one which is supposed to show up on the right side doesn't appear at all. Here is the code:
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"next" style:UIBarButtonItemStylePlain target:self action:#selector(nextResult:)]];
UIBarButtonItem *done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:#selector(showSearch:)];
[done setImage:[UIImage imageNamed:#"cancel"]];
[items addObject:done];
[toolbar setItems:items animated:NO];
You need to use UIBarButtonSystemItemFlexibleSpace instead of UIBarButtonSystemItemFixedSpace
Code:
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"next" style:UIBarButtonItemStylePlain target:self action:#selector(nextResult:)]];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"cancel" style:UIBarButtonItemStylePlain target:self action:#selector(showSearch:)]];
[toolbar setItems:items animated:NO];
Reference : UIBarButtonItem Class Reference
The item with the system item UIBarButtonSystemItemFixedSpace is a space, not button object. Therefore it does show up. It's just no button.
Change your code to
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"next" style:UIBarButtonItemStylePlain target:self action:#selector(nextResult:)]];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"cancel" style:UIBarButtonItemStylePlain target:self action:#selector(showSearch:)]];
[toolbar setItems:items animated:NO];

UIBarButtonItem does not respond to action event

I am using a UIToolBar and have added a UIBarButtonItem to it. When I click the button it does nothing. It does not respond to action event. This is my code :
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(x, y, width, toolBarHeight)];
[toolBar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(changeText:)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[self.view addSubview:toolBar];
Try this and see if this helps. Again your code looks fine.
// create an array for the buttons
NSMutableArray* BarbuttonsArray = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(changeText:)];
barButtonDone.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray addObject:barButtonDone];
[toolbar setItems:BarbuttonsArray animated:YES];
I literally copied and pasted your code into a test project of mine and this is what I get. The only change I did was the method name
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolBar setBarStyle:UIBarStyleDefault];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(changeText)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[self.view addSubview:toolBar];
}
-(IBAction) changeText
{
NSLog(#"changeText ...");
}
2014-01-25 13:59:15.882 001-test-proj[15596:a0b] changeText ...

blank Toolbar from setItems

My UINavigationController toolbar is blank and no items show up on it. Here is my code
self.navigationController.toolbarHidden = NO;
UIToolbar* toolbar = self.navigationController.toolbar;
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"Test"
style:UIBarButtonItemStylePlain
target:self
action:#selector(buttonPushed)]];
[toolbar setItems:items animated:YES];
That's not the proper way to setup the toolbar. UIViewController has a toolbarItems property. The nav controller will use that property to automatically populate its toolbar.
Your code should be:
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *btnTest = [[UIBarButtonItem alloc] initWithTitle:#"Test"
style:UIBarButtonItemStylePlain
target:self
action:#selector(buttonPushed)]];
self.toolbarItems = #[ btnTest ];

UIBarButtonItem not firing its action method

In viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES animated:NO];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Articles" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Source" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Aa" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Rabbit" style:UIBarButtonItemStyleBordered target:self action:nil]];
[toolBar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];
Method:
- (void)backButtonTapped {
[self.navigationController popViewControllerAnimated:YES];
}
I have a breakpoint on the call inside the method but it never gets called. Why is this method never getting called?
I had a UITapGestureRecognizer on the whole view that intercepted the tap on the UIBarButton. I solved it thanks to this answer, which basically stopped the UITapGestureRecognizer from beginning unless it was outside of the UIToolBar.
Your code is not complete and I dont know if you didn't implement it or just didn't wrote it here, so just to make sure, this should work:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
[toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:#"Articles" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)]];
toolBar.items = toolBarItems;
[self.view addSubview:toolBar];
Try like this. Its working fine.
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]initWithTitle:#"back"style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonTapped)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
[self.view addSubview:toolbar];

How to create Navigation using ToolBar button

I have created a Toolbar button and given action() to navigate to other ViewController.But the code is not working for Navigation.And not able to assign a image to the button.How can i solve this?
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setToolbarHidden:NO];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 418, 320, 44);
[self.view addSubview:toolbar];
[toolbar release];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"update" style:UIBarButtonItemStyleBordered target:self action:#selector(updateAddress:)];
NSMutableArray *toolbarItems = [NSMutableArray arrayWithObjects:customItem, nil];
[toolbar setItems:toolbarItems animated:NO];
}
-(void)updateAddress{
DisplayMapViewController *updateView=[[DisplayMapViewController alloc]init];
UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:updateView];
[self.navigationController popToViewController:navi animated:YES];
}
First you should set the selector right, you should take out the : from the selector because you don't have parameters for the updateAddress method
should be like this
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"update" style:UIBarButtonItemStyleBordered target:self action:#selector(updateAddress)];
Then You need to initialize the DisplayMapViewController with its NibName
DisplayMapViewController *updateView=[[DisplayMapViewController alloc]initWithNibName:#"DisplayMapViewController" bundle:nil];
and then just pop to the view you are intended to.
[self.navigationController popToViewController:updateView animated:YES];
if you have already rootViewController then you could just popToRootViewController

Resources