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

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

Related

iPhone UINavigationController bar buttons not showing

I am having an issue: I have a tab-bar based app. In MainWindow.xib for the tab that shows the nav controller it links to RootViewController.xib.
in RootViewController.xib I have created a navigation controller and have added a table view.
in RootViewController in viewDidLoad i have [self.view addSubview:navController.view];.
The tableview and navigation controller work well for navigation. pressing a cell pushes the controller, and bar buttons work in the pushed controller.
But when I use self.navController.navigationItem.rightBarButtonItem = barButton; nothing shows up at all. also changing self.navController.navigationItem to self..navigationItem ect.. doesn't help. What do you think the problem could be? I appreciate every answer.
fyi the barbuttonitem is setup with:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"Help" style:UIBarButtonItemStylePlain target:self action:#selector(help)];
If u want to use both controller navigation and tab bar controller the u check the sample code of apple documentation. In which both function are use so i given link below.
UIcatalogue
If you init the navigation controller with...
navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
Then you can set button with...
rootViewController.navigationItem.rightBarButtonItem = yourButton;

iPhone pushViewController keep the right part of navigation bar

In an iPhone app I want to have a navigation button on the right side of the navigation bar of the root navigation controller. However this right button disappears every time I push another viewController on top of the navigation controller and the right slot becomes empty. How can I make sure it remains on its proper place. I am using Interface Builder.
The "View Controller Programming Guide for iOS", as I understand it, says that the new viewController I'm pushing has to have its navigationController property set to the current navigation controller, however this property is readonly. How can I achieve this?
This is the thing with Navigation controller
The left bar button is taken Automatically by the iOS for All viewControllers
But you have to design the Rightbarbutton for the Specific ViewController
So for each ViewController in ViewDidLoad
UIBarButtonItem *rightCornerButton = [[UIBarButtonItem alloc]
initWithTitle:#"Client Profile"
style:UIBarButtonItemStylePlain target:self
action:#selector(onTouchRightBarButton:)];
[self.navigationItem setRightBarButtonItem:rightCornerButton];
[rightCornerButton release];

how to add items to the UINavigationBar

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

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.

iPhone: Is it possible to have an 'info'-button in all views in my app?

As mentioned in the title, how do I implement such a functionality?
I am using the code below, (in my viewDidLoad), to get the button on my Navigation Controller of my main view.
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(viewWillAppear:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
Not sure how I can get it displayed in all my views.
Each view pushed to the navigation stack has an own navigation bar. So I afraid you have to add this button to each navigation bar, when you create a view and before you push this view to the navigation stack.
You could simply add an info button to the window, instead of creating a new button for each view controller. Since the window never changes, the button will always be on screen. You only have to take care that no other views are on top of the button.
I would suggest you create an base view controller, and add the bar button to your navigationItem in viewDidLoad. Then all your view controllers subclass from the base controller, rather than the default UIViewController. This should solve your problem.