How to add UIBarButtonItem in UIToolBar in code - objective-c

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];

Related

Adding UIBarButtonItem's to a UIToolbar

I need to add buttons to the UIToolbar of a UINavigationController, which is also my root. I want the UIToolbar to appear when a specific UIViewController is shown. Therefore, I placed this code in my viewDidLoad method of my UIViewController subclass:
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithTitle:#"Title" style:UIBarButtonItemStyleBordered target:self action:#selector(doSomething)];
item.width = 300;
item.tintColor = [UIColor whiteColor];
UIBarButtonItem* item2 = [[UIBarButtonItem alloc] initWithTitle:#"Title2" style:UIBarButtonItemStyleBordered target:self action:#selector(doSomething)];
NSMutableArray* theItems = [self.navigationController.toolbar.items mutableCopy];
[theItems addObject:item];
[theItems addObject:item2];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
[self.navigationController setToolbarHidden:NO animated:YES];
[self.navigationController setToolbarItems:theItems animated:NO];
//self.navigationController.toolbarItems = theItems; // Tried both
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
label.text = #"kjhdkjhadsda";
[self.navigationController.toolbar addSubview:label];
This only shows the UILabel in the correct position, nothing else appears. The UILabel is useless for me, it is just a test. I also tried to allocate a new array instead of copying the one from the component. Ignore the missing releases, this is only test code.
I read many questions about this but no answer seem to help making it work. Any idea what might not be ok in this code?
Seems that you are trying make mutable copy of nil in line
[self.navigationController.toolbar.items mutableCopy];
By default method navigationController.toolbar.items nas no items and returns nil
Update
Method - (void)setToolbarItems:(NSArray*)toolbarItems animated:(BOOL)animated does nothing if you send it to UINavigationController. You need to set toolbar items to that controller who is managed by a navigation controller. This line will make your buttons visible:
[self setToolbarItems:theItems animated:NO];

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

How to add UIBarButtonItem to QLPreviewController

I already read this question QLPreviewController remove or add UIBarButtonItems but it's not what I'm looking for. I would like to keep the "Print" button in the navigation Bar but also add a new "Delete Document" button in the navigation bar.
I tried this:
QLPreviewController *previewer = [[[QLPreviewController alloc] init] autorelease];
[previewer setDataSource:self];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]initWithTitle:#"Salva Documento" style:UIBarButtonItemStyleBordered target:self action:#selector(saveFileToDocuments)];
NSArray *buttons = [NSArray arrayWithObjects:[[previewer navigationItem]rightBarButtonItem],saveButton, nil];
[[previewer navigationItem]setRightBarButtonItems:buttons];
But it didn't work.
Because you said "4.x will be fine", there's your problem.
The documentation for UINavigationItem [setRightBarButtonItems: animated:] (documentatin linked there for you) say that this function only works on iOS 5.0 and newer.
It will not work on iOS 4.0.
Also, you should add an animated: parameter to that setRightBarButtonItems: call.
UIBarButtonItem *rbb;
-(void)addRightButton{
if (!rbb) {
UIButton *orderButton = [UIButton buttonWithType:UIButtonTypeCustom];
orderButton.frame = CGRectZero;
rbb = [[UIBarButtonItem alloc] initWithCustomView:orderButton];
}
self.navigationItem.rightBarButtonItem = rbb;
}
- (void)viewDidLoad{
[super viewDidLoad];
[self performSelector:#selector(addRightButton) withObject:nil afterDelay:0.2];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self addRightButton];
}

Buttons on top of UIPopoverController

I want to add two Buttons on top of UIPopoverController like it is shown in following screenshots:
HTML Edit
Thanks for helping me!
Add your view controller to a UINavigationController, then add the Navigation Controller to the UIPopoverController. Then in your UIViewController's viewDidLoad method, put this code in:
UIBarButtonItem *okButton = [[UIBarButtonItem alloc] initWithTitle:#"Ok" style:UIBarButtonItemStyleBordered target:self action:#selector(okayButtonPressed)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelButtonPressed)];
self.navigationItem.title = #"My Title";
[self.navigationItem setLeftBarButtonItem:cancelButton animated:NO];
[self.navigationItem setRightBarButtonItem:okButton animated:NO];
[cancelButton release];
[okButton release];
You need to initialize your popover with a UINavigationController directly. Then set the root view to your custom view controller.
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
Use a UINavigationController as the pop-over. Then, access the .navigationBar property of the navigation controller, get the .topItem, and set its .leftBarButtonItem and .rightBarButtonItem.
I wouldnt use a navigationcontroller like the previous posters suggested, apple recommends not using navigationcontrollers on ipad (with good reason) it doesnt behave as youd expect when pushing VC into the stack when used in popovers, now you dont really want to use the "navigation" aspect of it, but i wouldnt use navigationcontroller just because uw ant the bar....Use a UIToolBar instead, and set its buttons to whatever you want...no need to use a navigation controller here...
When I do this my navBar doesn't seem to fit properly inside of the UIPopoverController, as shown in the below:
http://www.flickr.com/photos/coleorton/4752223066/
Here's what I'm doing:
// alloc the Direct Reports view controller.
ToolsViewController *toolsViewController = [[[ToolsViewController alloc] init] autorelease];
UINavigationController *toolsNavController = [[[UINavigationController alloc] initWithRootViewController:toolsViewController] autorelease];
toolsNavController.title = #"Tools";
toolsNavController.view.frame = CGRectMake(0.0, -10.0, 320.0, POPOVER_HEIGHT);
if(![self.toolsPopoverController isPopoverVisible]){
// show popover
self.toolsPopoverController = [[[UIPopoverController alloc] initWithContentViewController:toolsNavController] autorelease];
self.toolsPopoverController.delegate = self;
self.toolsPopoverController.popoverContentSize = CGSizeMake(320.0, POPOVER_HEIGHT);
[self.toolsPopoverController presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
} else {
// close popover
[self.toolsPopoverController dismissPopoverAnimated:YES];
}
This worked!
//Determine how to present this view based on device
if ([UIDevice currentDevice ].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
BNRAssetTypeViewController *contentViewController = [[BNRAssetTypeViewController alloc] init];
UINavigationController *popOverNavigation = [[UINavigationController alloc] initWithRootViewController:contentViewController];
self.assetPickerPopover = [[UIPopoverController alloc] initWithContentViewController:popOverNavigation];
[self.assetPickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
then in the init function of contentViewController add this
//add a barbutton item which will help in adding new type
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addNew:)];
//set bar item to right side of navbarite
self.navigationItem.rightBarButtonItem =bbi ;