how to add items to the UINavigationBar - iphone

I am new to iPhone development.
my application is based on UInavigationBar .
I want to add a navigation bar item in one of my xib, but in the .xib i just simulate the navigation bar so i can't drag and drop the item. thank you

You'll want to add the nav bar buttons programmatically. You see, your xib has a view that is shown within the content view of the UINavigationController. It is the UINavigationBar to which your app has access and which controls the nav bar items. As you point out, your xib has just a placeholder for the nav bar, which is really a convenience for you so your view is sized correctly as you lay it out.
In your UIViewController for the xib, you an add view-appropriate nav bar items with code something like
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"View" style:UIBarButtonItemStylePlain
target:self
action:#selector(viewControls:)] autorelease];
Does that make sense?

In order to be able to add items into a UINavigationBar, you need to first add a UINavigationBar to your view and then add items to it.
You cannot drag and drop items on a simulated navigation bar. A simulated Navigation Bar is just there to make sure you have a correct estimate of the view size available to you if you are adding a Navigation Bar by some other means or from code.

You should be using a UINavigationController for a navigation based hierarchy. That will take care of a lot of the lower details of how to make navigation work as you would like it to. I would also recommend setting that all up programmatically. Here is how you would do that.
// Initial setup of navigation
YourViewController *yvc = [[YourViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:yvc];
[[self view] addSubview:[nav view]];
Then when you want to go to a new view controller (the animated sliding that you normally see), you do this
// From inside 'YourViewController',
// this is normally when the user touches a table view cell
NewViewController *nvc = [[NewViewController alloc] init];
[self.navigationController pushViewController:nvc];
If you want to change the title or the buttons, you do this
// This is normally in viewDidLoad or something similar
[self.navigationItem setTitle:#"Hello World!"];
[self.navigationItem.rightBarButtonItem:/* A UIBarButtonItem */];

Related

iPhone: a same bottom bar for several views

I would like to add a bottom bar in a view and then keep it there while I browse several views (each one with a nab bar). I hope it will follow Apple guidelines (I already know that only the nab bar is recommended) or at least the app is accepted.
For doing so, I have added a UIView to the application window. This UIView contains a UITabBarController which contains the navigation controllers (each one as a rootviewController) of each one of the items of the bar:
UIWindow *window=[[UIApplication sharedApplication] keyWindow];
MyUIViewController *mv=[[MyUIViewController alloc]init];
UINavigationController *navd = [[UINavigationController alloc] initWithRootViewController:mv];
navd.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"MyItem" image:[UIImage imageNamed:#"myImage.png"] tag:0];
NSMutableArray *controllers = [[NSMutableArray alloc] init];
[controllers addObject:navd];
UITabBarController *tbarController=[[UITabBarController alloc] init];
tbarController.viewControllers = controllers;
tbarController.customizableViewControllers = controllers;
UIView *vistaBarraInferior=tbarController.view;
[window addSubview:vistaBarraInferior];
[window makeKeyAndVisible];
It works, but my problem appears when I would like to go back and exit the ivies with UITabBarController. If I write:
[self.tabBarController.view removeFromSuperview];
[self.parentViewController.navigationController popViewControllerAnimated:YES];
The tab bar is removed from the current view, but I canĀ“t reach the previous view (the root view I had had before doing anything), because I have overwritten it with the 'initWithRootViewController'.
Is it any other way to make it easier or make this work out?
Thanks in advance.
If you want to combine tab bar and navigation bar you must use tab bar as a root and for each tab item add one navigation controller. You can't do it in opposite way as tab bar must be always root.
I am not sure what do you want achieve with bottom bar. Whether it should serve as a tab bar or toolbar. Remember if you use tab bar then each tab will have its own stack of views managed by navigation controller. On the other hand toolbar is related to one view.
If you want to create something like overlay toolbar - something that is always on top of views even if they are animating in and out - you need to choose different solution.
For example you can create your own controller that will display your toolbar at bottom and some container view which content will be managed by navigation controller.
You should use a tabbarcontroller with array of navigation controllers. See apple sample code here for example.
I went through your code and it does not look right on different levels. For example you are calling initwithviewcontroller but passing a uiview. That is not correct.
If I rewrite this code in other way:
UITabBarController *tabBar=[[UITabBarController alloc]init];
tabBar.title=#"MyTitle";
NSMutableArray *items=[[NSMutableArray alloc]init];
MyUIViewController *ld = [[MyUIViewController alloc] init];
[tabBar addChildViewController:ld];
UITabBarItem *tabItem=[[UITabBarItem alloc]initWithTitle:#"Item1" image:[UIImage imageNamed:#"myImage.png"] tag:0];
[items addObject:tabItem];
[tabItem release];
[tabBar setToolbarItems:items];
[self.navigationController pushViewController:tabBar animated:YES];
The result is that the tab bar at the bottom is empty. Have I forgotten to write anything else to show the tab bar item?
If I add a second item, only its title is shown, but neither its image nor anything related to the first item is shown.

Switch between different 'child' view controllers using a toolbar and its buttons

Does anyone have any tips for this scenario.
My app delegate's nib has a viewcontroller set as the rootviewcontroller, so it loads this view controller when the app loads.
This viewcontroller has a toolbar with various buttons. These buttons are meant to switch between different view controllers.
I have tried using addChildViewController, presentViewController, presentModalViewController nothing allows me to switch between view controllers BUT still keep the toolbar visible.
If I use addSubView then all the orientation stuff goes mental and I have to resize the view controller manually which doesn't seem like something I should be doing.
EDIT: I want to keep the nav controller's button visible even when pushing controllers on i.e. if I have an EDIT and DELETE button I want those same buttons to remain on the toolbar even when I push different controllers onto the stack
It sounds like you should be using a tab bar.
Alternatively, you should be using a navigation controller with a toolbar and push/pop view controllers on this when the toolbar buttons are pressed:
Please note the navigation bar does not have to be visible if you use a navigation controller.
//create first button
buttonOne = [[UIBarButtonItem alloc] initWithTitle:#"EDIT" style:UIBarButtonItemStyleBordered target:self action:#selector(editStuff)];
[buttons addObject:buttonOne];
//create second button
buttonTwo = [[UIBarButtonItem alloc] initWithTitle:#"DELETE" style:UIBarButtonItemStyleBordered target:self action:#selector(deleteStuff)];
[buttons addObject:buttonTwo];
// Add buttons to toolbar and toolbar to nav bar.
[buttonsToolbar setItems:buttons animated:NO];
[buttons release];
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:buttonsToolbar];
self.navigationItem.leftBarButtonItem = twoButtons;
[twoButtons release];
addSubview: doesn't permit use of a secondary view controller, so that's not ideal.
You can use a toolbar with bar buttons to switch view controllers, but the simplest implementation is to have identical toolbars in each view controller's nib, and make the view controllers subclasses of a superclass that handles all of the toolbar actions.
There is no need to use a tab bar or navigation bar, although either of these would be a simpler approach in someways (but less obvious in the ways that matter).
Use a UITabBarController as your root controller. This acts as a parent container for your child viewcontrollers, provides a tab bar and implements switching between child views. Check out the class reference or the View Controller Programming Guide

iOS - how to create a navigation bar item in view controller?

I have been trying to create a navigation bar back button.
Here is my code :-
UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:nil]autorelease];
self.navigationItem.rightBarButtonItem = barButton;
But it isn't displaying anything on navigation bar.
I am using UIViewController, not an UINavigationController.
Is UINavigationController is the only way to achieve this?
Any help would be really appreciated.
Any link to a good tutorial will be great.
Thanks.
Without the viewcontroller having a navigation controller (i.e viewController.navigationController != nil) you cannot add it in this manner.
One thing you can do is if it is being created by the nib is to just drag a bar button item into a navigation bar and link it via IBAction.
I'd recommend pushing this view controller out of a nvigationcontroller - you will get all those things for free:
UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:vc];
If you have your view controller being created from a NIB file in Interface Builder, and the view controller's design surface has a navigation bar on it, the easiest way to do this would be to drag a bar button item from the objects inspector right onto the navigation bar's right side. You would then create an IBAction in your header and implementation that you would hook up to.
I achieve this inserting the button directly in the UINavigationBar:
[yourUINavBar insertSubview:yourButton atIndex:1];
UIViewController's -navigationItem method does only work together with UINavigationController or other UIViewController containments.
You will have to get access to the UINavigationBar and set the item directly.
If you want to have a navigation bar, and have it work as you expect, create a UINavigationController using your UIViewController as the root view controller. Use that UINavigationController where you are using your UIViewController now.
Check out UINavigationController at developer.apple.com for more details.

UINavigationController Toolbar Buttons

I have a UINavigationController that I've set as the rootViewController of my window. In the NIB file I've set it up so that it has a "Bottom Bar" of "Toolbar". In Interface Builder I've added a UIBarButtonItem. This all works great and I can handle the button click fine. When I hit one of the buttons, I push a new view onto the ViewController and that works fine too. One problem, my button disappears when the view is loaded. Now in the subsequent view I can set the bottom bar to be a Toolbar and I see it in Interface Builder, but I cannot add any buttons to it.
I know I'm either missing something obvious or thinking about this incorrectly but how do I add UIBarButtonItems to subsequent views pushed to my nav controller? I'm talking the bar at the bottom, not the nav bar at the top.
Any ideas?
The toolbarItems property on the UIViewController is what you are interested in. You can create UIBarButtonItems programmatically and add them to a new toolBarItems array in viewDidLoad.
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* editButton = [[[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editAction)] autorelease];
[self setToolbarItems:[NSArray arrayWithObject:editButton]];
}
This worked better for me:
[[self navigationItem] setLeftBarButtonItem:homeButton];
You can do the same for the right side.

UIToolbar buttons disappear when pushing new view onto navigation stack

I have an iPhone app based around a UINavigationController with a UIToolbar at the bottom with various buttons in it that I created through the Interface Builder. When I use [navigationController pushViewController:animated:] my new view slides into place as expected but then all of the buttons are disappearing from the toolbar - the toolbar itself stays visible, it's just completely empty.
How do I get the buttons to stay put?
Here's the bit where I respond to the user pressing one of the toolbar buttons that then shows the new view:
- (IBAction)clickSettings:(id)sender {
NSLog(#"Clicked on 'Settings' button");
SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:#"Settings" bundle:nil];
[navigationController pushViewController:settingsViewController animated:YES];
}
The tool bar buttons are the property of a given view; when you push a new view on to the navigation stack, the tool bar buttons of the new view will slide in to place.
The tool bar itself seems to "belong" to the navigation controller; visibility of the toolbar is controlled by the UINavigationController toolbarHidden property, i.e.,
self.navigationController.toolbarHidden = YES;
To actually keep the toolbar from one view to the next, you can copy the toolbarItems property from one UIView to the next.