here in my application i want to add barbuttonitem in tab bar.
i displayed tab bar in view but i dont no how to add barbuttonitem programmaticaly in iphone.
can any one plz give me information about my problem.
thank you in advance..
You can not add bar button items to tab bar.
To add bar button item to a toolbar
//Create a BarButtonItem configure the way you want.
self.forwardButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
target:nil action:nil];
// Create an array for all the bar buttons
NSArray *toolBarItems = [[NSArray alloc] initWithObjects:forwardButton,nil];
// Set toolar items
[self.toolBar setItems:toolBarItems];
// add items to toolbar
[self addSubview:self.toolBar];
EDIT
In case you want to add space between BarButtonItems
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
You cannot add barbuttonitem in tab bar. You can add barbuttonitem in tool bar. for that check out this link
http://www.iphonedevsdk.com/forum/iphone-sdk-development/59285-programmatically-add-button-uitoolbar.html
Related
How can a button be added to a navigation toolbar in an application which is navigation based?
The rootController is a tableView by default, and this is perfect. I would like to add a "share" button to the navigation controller so that I can attach it to my own method.
How is this done if the navigation bar is added in the code somewhere?
To do it in code, go to your views viewDidLoad method and create a UIBarButtonItem
This code will create a button that says share like you wanted and put it on the right hand side of the navigation bar.
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithTitle:#"Share" style:UIBarButtonItemStyleBordered target:self action:#selector(share)];
self.navigationItem.rightBarButtonItem = shareButton;
[shareButton release];
}
UIBarButtonItem *shareButton = [[[UIBarButtonItem alloc] initWithTitle:#"Share" style:UIBarButtonItemStylePlain target:self action:#selector(yourShareAction)] autorelease];
self.navigationItem.rightBarButtonItem = shareButton;
This makes an add button, but you get the idea.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:
self action:#selector(buttonPressed:)]
autorelease];
You can create an UIBarButtonItem and attach it to a root view controller's navigation item. You add a target-action pair when you initialize the UIBarButtonItem object and that action message will be sent to the target on a user tapping on the button.
So if you want the button when the table view is shown, set the UIBarButtonItem to your table view controller's navigationItem.rightBarButtomItem property.
i'm having 3 screens
set title to screen1 using
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title =#"Products";
}
when navigation to next screen (screen2) second screen having the title "Categories"
but there is a button on the top name "Products" to Screen1
i want to change the button name as "Back"
Please tell me how?
triedself.navigationItem.leftBarButtonItem .title = #"Back";
See these two:
How to set the text of a back button on a UINavigationBar?
How do I change the title of the "back" button on a Navigation Bar
Solves your problem.
UIBarButtonItem *item=self.navigationItem.leftBarButtonItem;
item=[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem=item;
[item release];
Put this code in your first scree viewDidLoad method as it is It really works.
You have to do something like the follows to set the text of the back bar button to something other than what the previous view was named:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = #"Your Custom Title";
self.navigationItem.backBarButtonItem = barButton;
You CANNOT do the following:
self.navigationItem.backBarButtonItem.title = #"Your Title";
This sets the previous view on the stack to "Your Title" and not was it was before.
Hey, I am trying to add bar button items to my UINavigationBar (nav bar) but I have found out that bar button items are not properties of navigation bar and hence can't be accessed directly like:
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonSystemItemDone target:nil action:nil];
navBar.rightBarButtonItem =rightButton;
I am creating my navigation bar programmatically.
Apparently, there is something called UINavigation Item that has to be dealt with to add bar buttons. Can anyone tell me how to go about this? How to create this UINavigation item programmatically and add this to my navBar and then add the bar buttons to it.
UINavigationBars shouldn't be used on their own, outside of a UINavigationController.
When a navigation controller is used, each view controller on the navigation stack has a UINavigationItem. It is this UINavigationItem that you can add bar button items to.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonSystemItemDone
target:nil action:nil] autorelease];
I'm trying to either add 3 custom buttons to my navigation controller toolbar on the top of my view or add a segmented control with 3 options. I have the following code on my app delegate for when i create my view controller(fwc) but the buttons dont appear.
/*
Set up the navigation controller for the Feeding Tab
*/
// instantiate the feedingViewController and set the title to Feedings
feedingViewController *fwc =
[[feedingViewController alloc] initWithNibName:#"feedingViewController"
bundle:[NSBundle mainBundle]];
//fwc.title = #"Feedings";
// set the tab bar item up and add it as feedingViewController's tab bar item
UITabBarItem *feedingTabBarItem =
[[UITabBarItem alloc] initWithTitle:#"Feedings" image:nil tag:0];
fwc.tabBarItem = feedingTabBarItem;
[feedingTabBarItem release];
// create a new nav controller for feedings and add root view
feedingNavController = [[UINavigationController alloc] init];
//Create the add button, need to change the selector to something though *****
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(newFeeding)];
//self.navigationItem.rightBarButtonItem = add;
UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
// Create and configure the segmented control
UISegmentedControl *sortToggle = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:#"Ascending",#"Descending", nil]];
sortToggle.segmentedControlStyle = UISegmentedControlStyleBar;
sortToggle.selectedSegmentIndex = 0;
[sortToggle addTarget:self action:#selector(toggleSorting:)forControlEvents:UIControlEventValueChanged];
// Create the bar button item for the segmented control
UIBarButtonItem *sortToggleButtonItem = [[UIBarButtonItem alloc]initWithCustomView:sortToggle];
[sortToggle release];
// Set our toolbar items
feedingNavController.toolbarItems = [NSArray arrayWithObjects:
flexibleSpaceButtonItem,
sortToggleButtonItem,
flexibleSpaceButtonItem,
add,
nil];
feedingNavController.navigationController.navigationBarHidden=NO;
[sortToggleButtonItem release];
[add release];
// Push the feedingViewController on the nav stack and release it.
[feedingNavController pushViewController:fwc animated:NO];
[fwc release];
In order to use a UITabBar you would need a UITabBarController, which is different than the UINavigationController. A UITabBar has a fundamentally different use than a UISegmentedControl. It appears that the functionality you're trying to implement is not appropriate for a UITabBar. In your question description you mention trying to add these buttons to the "navigation controller toolbar on the top." A UINavigationController has a UINavigationBar, which is the bar that runs across the top, and a UIToolbar, which is the bar that appears at the bottom. The UIToolbar, by default, is set to hidden, but you get a UIToolbar for free whenever you create a UINavigationController (see the UINavigationController reference in Xcode).
Apple's NavBar demo shows how to put a UISegmentedControl into the UINavigationBar. Instead of a title, use a custom titleView to display the segmented control:
fwc.navigationItem.titleView = sortToggle;
If you want to put your add UIBarButtonItem in the UINavigationBar as well, you can use:
fwc.navigationItem.rightBarButtonItem = add;
Note that you shouldn't actually go about trying to customize the UINavigationController's navigation bar on your own. The proper way to customize is to have an individual view controller access it's own navigationItem and set the titleView and rightBarButtonItem with the items you want.
If you wish to approach your problem using a UIToolBar instead, meaning that your items will appear on the bottom of the screen, you can do something like this:
// Assume UIBarButtonItem *add, UIBarButtonItem *sortToggleButtonItem,
// and UIBarButtonItem *flexibleSpaceButtonItem are allocated
[fwc setToolbarItems:[NSArray arrayWithObjects:
flexibleSpaceButtonItem,
sortToggleButtonItem,
flexibleSpaceButtonItem,
add,
nil]];
[feedingNavController setToolbarHidden:NO];
i have a problem while adding button to the navigation bar.. My application consist of two view controllers added to the viewControllers array of a tabBarController. Inturn this tabBarController is added to the viewControllers array of a navigationController. In one of the views i have a textfield for entering dates . On the tap of this textfield the datepicker will pop up. simultaneously i want to display a done button on the navigation bar. how can i do this... i tried using
self.tabBarController.navigationController.navigationItem.rightBarButtonItem = self.doneButton;
But this is not working...
Pls help me out..
Try this code,
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Done", #"")
style:UIBarButtonItemStyleBordered
target:self
action:#selector(DoneButton)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
Best of luck.
Try with this
UIBarButtonItem* _doneButton;
self.navigationItem.rightBarButtonItem = _doneButton;
use this one:
UIButton *myBtn = [[UIButton alloc]init];
[self.navigationItem.titleView addSubview:myBtn];