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

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.

Related

why setBackBarButtonItem not work in the childViewController viewDIdLoad?

I want to custom the backBarButtonItem in the navigationItem,
here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:back];
}
but it didn't work while I used the leftBarButtionItem can work.
I don't know ,can anybody give me an answer?
- (void)viewDidLoad
{
[self.navigationItem setHidesBackButton:YES];
UIBarButtonItem *cancelNavButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(dismissController)];
[self.navigationItem setLeftBarButtonItem:cancelNavButton];
}
- (void)dismissController
{
[self.navigationController popViewControllerAnimated:YES];
}
You shouldn't have to create the back button yourself if you are pushing your view controllers using the UINavigationController's pushViewController:animated: method. This will push the new ViewController on the NavigationControllers stack and give you an automatic back button with the title of the previous ViewController.
The reason your button doesn't work is the target: and action: parameters are nil you could bind them to the UINavigationController's popViewControllerAnimated: method to achieve the same action as the default back button.
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:[self navigationController]
action:#selector(popViewControllerAnimated:)
];
[[self navigationItem] setBackBarButtonItem:back];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.leftBarButtonItem = back;
}

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

how to add a rightbarbuttonitem in specific view of tabbarcontroller

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;

Adding UIButton top bar of a navigation controller that pushes to another viewController

I have a UIButton that pushes to another view controller using the following code. How do I go about putting this UIButton to the top bar of a navigation controller.
-(IBAction) nextButtonPressed {
TipsViewController *objYourViewController = [[TipsViewController alloc] initWithNibName:#"TipsViewController" bundle:nil];
[self.navigationController pushViewController:objYourViewController animated:YES];
[TipsViewController release];
}
I was thinking of adding something like: self.navigationItem.rightBarButtonItem somewhere, but can't figure out the syntax.
Here's something else I tried with the following code: I can get a "Next" button in the top bar of the Navigation Controller. How would I get it to push to another view controller?
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self pushViewController:anotherButton animated:YES];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
thanks for any help.
When you init your UIBarButtonItem, do you see a parameter called action: ? Mention a selector there:
UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(Next:)];
The above item should be your rightBarButton as you want it to be.
And this would be your selector:
-(void)Next:(id)sender {
[self.navigationController pushViewController:nextViewController animated:YES];
}
UIBarButtonItem addButton=[[UIBarButtonItem alloc]initWithTitle:#"+" style:UIBarButtonItemStyleBordered target:self action:#selector(method)];

trying to programmatically create rightBarButtonItem

This doesn't seem to be working. What am i doing wrong?
-(void)awakeFromNib{
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(showNewEventViewController)];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
NSLog(#"awaked");
[rightBarButtonItem release];
}
my guess is, that you add the UIBarButtonItem to the wrong object!
you need to add it, to the rootViewController (instead to the UINavigationController, as you probably did)
YourRootViewController *theRootController = [[YourRootViewController alloc] init];
UINavigationController* navContainer = [[UINavigationController alloc] initWithRootViewController:theRootController];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(dismiss)];
theRootController.navigationItem.rightBarButtonItem = btnCancel
[navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:navContainer animated:YES];
I would normally put this code in the viewDidLoad method rather than the awakeFromNib method; I'm not sure if that's where your problem lies. What does "not working" mean?
Try this instead:
- (void) initUI {
UIBarButtonItem *btnCancel = [[[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(dismiss)]autorelease];
self.navigationItem.rightBarButtonItem = btnCancel;
//[btnCancel release]; no need to explicitly release the item
}