how to add a rightbarbuttonitem in specific view of tabbarcontroller - iphone

as my title, how do i add in a rightbarbuttontiem in a specific view of tabbarcontroller ?
i'm using storyboard to create the tabarcontroller.
i tried this but the item are not displayed
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"gear.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(pushToSetting)];
//BarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = settingButton;

Are you using a NavigationController? If you are, then your code should work. If you are using just a UINavigationBar, then you have to set the items property for the UINavigationBar. As you are not getting a rightbuttonitem, I assume you are using a UINavigationBar. This code should work:
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStylePlain target:self action:nil];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Test"];
navItem.rightBarButtonItem = settingButton;
NSArray *navItems = [NSArray arrayWithObject:navItem];
[self.navBar setItems:navItems];
You should have the rightbarbuttonitem now. Here, navBar is a UINavigationBar outlet. You can also pop and push navigation items onto a UINavigationBar using these methods:
(void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
(UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;

Related

How to add UIButtons to navigationbar ,the class also consists UITabbarController

I am new at iOS Development.I Have UINavigationController as rootviewcontroller for UIWindow.In the subclass i added UITabbarController programatically.I given default Tabbarcontroller selection as first controller. Here i am trying to add 3 buttons to the navigationbar .
Can any send me the corrent code.
Thanks In advance
To add more than one button on the left or the right side you have to call one of the following methods:
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated
- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated
The array items contains instances of the UIBarButtonItem class. You can instantiate them like this
UIBarButtonItem *FirstButton = [[UIBarButtonItem alloc]
initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(buttonTapped:)];
Then you have to implement the selector buttonTapped: which is called when the button has been tapped.
-(void)buttonTapped:(UIBarButtonItem *)button
{
// do the things that should happen when the button is pressed
}
If you don't want to get them on the left or right side it is also possible to create a UIView by yourself containing the buttons and set the view to be the titleView of the UINavigationItem. This effect is similar to the buttons in the Facebook App
[self.navigationItem setTitleView:yourView];
Here is simple code for add UIButton on UINavigationBar
In Following code you can add Button With FlexibleSpace
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *btnFirst = [[UIBarButtonItem alloc] initWithTitle:#"First" style:UIBarButtonItemStyleBordered target:self action:#selector(FirstTapped:)];
[barItems addObject: btnFirst];
UIBarButtonItem *btnSecond = [[UIBarButtonItem alloc] initWithTitle:#"Second" style:UIBarButtonItemStyleBordered target:self action:#selector(SecondTapped:)];
[barItems addObject:btnSecond];
UIBarButtonItem *btnThird = [[UIBarButtonItem alloc] initWithTitle:#"Third" style:UIBarButtonItemStyleBordered target:self action:#selector(ThirdTapped:)];
[barItems addObject: btnThird];
self.navigationItem.rightBarButtonItems = barItems;
Button Related Methods
-(void) FirstTapped:(UIBarButtonItem *)sender{
//perform your action
}
-(void) SecondTapped:(UIBarButtonItem *)sender{
//perform your action
}
-(void) ThirdTapped:(UIBarButtonItem *)sender{
//perform your action
}
NOTE : self.navigationItem.rightBarButtonItems Works only in iOS 5 or Latter.
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
// here is custom button setup //
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:customButton];
[self.navigationItem setLeftBarButtonItem:item animated:YES];

add navigation bar programmatically to a tableviewController (segue push or modal )

I'm trying to programmatically add a navigationBar to a tableViewController :
When I come from a PUSH Segue the builder adds a navigation bar with navigation items, like I want.
But the problem is that when i come form a MODAL Segue I try to add a bar with code like this:
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, -260, 320, 44)];
[self.view addSubview:naviBarObj];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelButtonPressed)];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneButtonPressed)];
UINavigationItem *navigItem = [[UINavigationItem alloc] initWithTitle:#"Navigation Title"];
navigItem.rightBarButtonItem = doneItem;
navigItem.leftBarButtonItem = cancelItem;
naviBarObj.items = [NSArray arrayWithObjects: navigItem,nil];
....
- (void) cancelButtonPressed { }
- (void) doneButtonPressed { }
But nothing appears... No top bar.
naviBarObj's frame ypoint is -260, maybe this is making nothing appear.

multiple rightbar buttons in navigationbar in xcode4.2

I want to create three buttons in right side of Navigation bar.I am using storyboard for creating the UIView Controller.
In DetailViewController am Embedded a Navigation bar using storyboard and then creating three UIBarButtonItem programetically and then adding these in an array then assigning it to navigationitem.This is working fine.
UIBarButtonItem *Button1 = [[UIBarButtonItem alloc]initWithTitle:#"Button1" style:UIBarButtonItemStylePlain
target:self action:#selector(Button1Clicked:)] ;
UIBarButtonItem *Button2 = [[UIBarButtonItem alloc] initWithTitle:#"Button2" style:UIBarButtonItemStylePlain
target:self action:#selector(Button2Clicked:)] ;
UIBarButtonItem *Button3 = [[UIBarButtonItem alloc] initWithTitle:#"Button3" style:UIBarButtonItemStylePlain
target:self action:#selector(Button3Clicked::)] ;
self.navigationItem.rightBarButtonItems =
[NSArray arrayWithObjects:Button1,Button2,Button3, nil];
I have another viewcontroller this is a modalviewcontreoller. I am creating the view controller using storyboard and adding a navigationbar not a navigationcontroller.Then using the same method for adding buttons to navigtion bar, but not shown any buttons.
Plese anyone know how to solve this issue.?
If your viewController is in UINavigationController you can simply use
self.navigationItem ...
which works fine as I see. But if your viewController (your modal in this case) is not in UINavController, you have to access UINavigationItem in this way:
someNavigationBar.topItem ...
So, if you set an IBOutlet to your navigationBar, your code should look like this one:
UIBarButtonItem *Button1 = ...
UIBarButtonItem *Button2 = ...
UIBarButtonItem *Button3 = ...
yourNavigationBar.topItem.rightBarButtonItems = [NSArray arrayWithObjects:Button1, Button2, Button3, nil];
Please add UINavigationBar instead of UINavigationItem

UINavigationController Toolbar - code once, not in every view controller?

I have an app with a navcontroller created in the appdel. Each vc pushed in has a block of code in the viewdidload that sets up the toolbar. The toolbar is always the same. Is there a way for me to just create this code once - and not put it in every vc?
[self.navigationItem setHidesBackButton:YES];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStyleBordered target:self action:#selector(backClicked)];
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *storyBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Sto" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
storyBtnItem.tag = 1;
UIBarButtonItem *renderBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Ren" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
renderBtnItem.tag = 2;
UIBarButtonItem *amenBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Ame" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
amenBtnItem.tag = 3;
UIBarButtonItem *availBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Availability" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
availBtnItem.tag = 4;
UIBarButtonItem *eopBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Eq" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
eopBtnItem.tag = 5;
UIBarButtonItem *stkBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"St" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
stkBtnItem.tag = 6;
UIBarButtonItem *movBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Fi" style:UIBarButtonItemStyleBordered target:self action:#selector(toolbarControl:)];
movBtnItem.tag = 7;
NSArray *items = [NSArray arrayWithObjects:flexibleSpaceLeft, stoBtnItem, reBtnItem, ameBtnItem, avaBtnItem, eBtnItem, stBtnItem, mvBtnItem, nil];
[self setToolbarItems:items];
[self.navigationController.toolbar setTintColor:[UIColor colorWithRed:79.0/255.0 green:145.0/255.0 blue:205.0/255.0 alpha:1.0]];
Just do vc.toolbarItems = self.toolbarItems (where vc is the view controller to be pushed) in the method where you are pushing the next view controller.
eg:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *vc = [UIViewController new];
vc.toolbarItems = self.toolbarItems
[self.navigationController pushViewController:vc animated:YES];
[vc release]; // if not using ARC
}
Also, you don't need to do this in the -viewDidLoad method, setting the navigation items and toolbar items does not require the view to be loaded and can thus be done in your init or awakeFromNib method. If you do it in -viewDidLoad you are potentially setting the items multiple times.
The way i usually handle those sort of things is to create a base view controller, and the code to setup the toolbar will be in the base view controller, and all other view controllers inherit from this base controller.
You could make an abstract sub-class for your viewControllers that has this function in its ViewDidLoad. Then you just adjust it in one place.
For example, make a view controller class called myMasterViewController. All you have to set up in it is the viewDidLoadMethod. Then in your other controllers make them inherit from myMasterViewController instead of UIViewController. Make sure you have [super viewDidLoad]; in your other VC's.

How to remove a Subview from navigationController?

I added a toolBar with this code:
- (void)viewWillAppear:(BOOL)animated {
UIBarButtonItem *yesterday = [[UIBarButtonItem alloc]initWithTitle:#"Yesterday"
style:UIBarButtonItemStyleBordered target:self action:#selector(yesterday:)];
UIBarButtonItem *today = [[UIBarButtonItem alloc]initWithTitle:#"Today"
style:UIBarButtonItemStyleDone target:self action:#selector(today:)];
UIBarButtonItem *tomorrow = [[UIBarButtonItem alloc]initWithTitle:#"Tomorrow"
style:UIBarButtonItemStyleBordered target:self action:#selector(tomorrow:)];
UIBarButtonItem *month = [[UIBarButtonItem alloc]initWithTitle:#"Month"
style:UIBarButtonItemStyleBordered target:self action:#selector(month:)];
NSArray *items = [NSArray arrayWithObjects:yesterday,today,tomorrow,month, nil];
[yesterday release];
[today release];
[tomorrow release];
[month release];
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];
[toolbar setFrame:CGRectMake( 0, 20, 320, 40)];
[toolbar setItems:items];
[self.navigationController.view addSubview:toolbar];
}
but when i change the view using the navigation controller the toolbar stays there...
how can i remove that subview?
UINavigationController has a toolbar built in which is hidden by default. You can display it using [navigationController setNavigationBarHidden:animated:];. You might want to use that instead. Then, before you push a view controller, set that view controller's hidesBottomBarWhenPushed property to true.
The reason your toolbar doesn't go away in this instance is that you're adding it to navigationController's view which displays on top of other views it controls. You could instead add it as a subview of self.
To answer your specific question, though, to remove the toolbar from any superview, use [toolbar removeFromSuperview]. In this case, I would go with the cleaner solution of using the toolbar that is built into navigation controllers.