iPhone Sdk: How to add a second UINavigationController? - iphone

i created an app from the navigation-based template given by apple. Now i want to add a second navigation controller to my application including a new UITableView. Can anybody show my how to do this? Thanks!

I think this can be done. In your app delegate you normally do something like
[window addSubview:navController.view]. UIWindow is just a UIView. So if you create two UIView ivars in the UIViewController that will contain the two nav controllers you should be able to do a similar thing:
#interface MyViewController : UIViewController
{
UIView* upperView;
UIView* lowerView;
}
etc...
MyUpperRootViewController* myUpperRVC = [[MyUpperRootViewController alloc] init...
UINavigationController* myUpperNavController = [[UINavigationController alloc] initWithRootViewController:myUpperRVC];
[upperView addSubview:navController.view];
[myUpperRVC release];
and something similar on lowerView.
In the root view or subseqeunt views pushed onto the controllers access them in the usual way as if there was one nav controller. [self.navigationController push... should behave normally.
For animating in (and out) the view controllers, just apply the animation to the views - upperView or lowerView. You might want to start with their frames off the visible display and then change them to something visible inside an animation block.

Related

Switching from one view to second view?

I want to know that can in switch between two views in an iPhone application if I have chosen the application as window based application in the Xcode or it is only possible to switch between views in view based application only.
How to design interface for changing the views in such appliocations as I am not able to design the second view in the interface builder after designing the first view.
Your view controller can present any other view controller like this
[firstViewController presentModalViewController:secondViewController animated:YES];
This will take you to second view controller.
To come back to first view controller, in second view controller you say
[secondViewController.parentViewController dismissModalViewControllerAnimated:YES];
Please refer to the documentation here
The main problem i think you have here is the perception of what each project type does.
A Window based application provides just a window and no "default" view controller for you to use.
A View based application provides a window AND a view controller and xib file for you to create your UI.
If you want to see how to add a view to a window based application create an empty view based application and look at the code that is auto added to the didFinishLaunchingWithOptions method in the appdelegate. This is essentially what you need to do with your window based application.
Add a view controller with a xib file for user interface, then look at how the view based application loads this view and displays it (using initWithNibName and then adding the view to the window)
i'd say you need to do more reading: take a look at cocoa fundamentals for iOS - in the documentation and then the view controller programming guide) these are both essential areas of reading. Then have a root around in the standard project types and take a look at how they are set up, this is really useful because you'll see what apple intend you to do when setting up your app
When you have your class which controls the UIWindow, you add objects in that window. One or some of these objects are Navigation Controllers or Views. in you windowController.h, you should define a view:
#property (nonatomic, retain) IBOutlet UIView *mainView;
and in the .m-file, synthesize it:
#synthesize mainView;
Then use it:
MainView *mainView = [[MainView alloc] initWithNibName:nil bundle:nil];
mainView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:mainView animated:YES];
[mainView release];
That is one of the two things that could possibly be your question.
Another question you could ask is: how to switch from my window-based application to a view-based application?
You can just create new classes with corresponding .xib-files, being UIView's. Adapt your appDelegate classes and you should be fine.
Switching from one view to other in view based application
FirstViewController *firstViewController = [[UIViewController alloc] initWithNibName:#"FirstViewController" bundle:[NSBundle mainBundle]];
[self.view addSubview:firstViewController.view];
or you can also use this.
FirstViewController *firstViewController = [[UIViewController alloc] initWithNibName:#"FirstViewController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:firstViewController animated:YES]; // this is deprecated in ios6.0

TabBarController + TableViewController + Navigation Controller?

I'm new to iphone development, and I want to make an app combining TabBarController + UITableViewController + UINavigationController. I know this question was widely discussed in some video tutorials and threads, but some of them are for old xcode and sdk versions, and I have problems when working through the tutorials.
I will be glad if someone could help me with an up to date tutorial or source files.
To be clear, I'm trying to have my app based on a tab bar controller with two tabs: the second will be a simple view, and the first should contain a tableviewcontroller and a navigation controller. When I click on a cell, it should move to another "detail" view.
Any help will be great.
Ok this is going be long:
in the AppDelegate.h
allocate a UITabBarController a UINavigationController and 2 UIViewControllers
for example:
UITabBarController *mainTabBar;
UINavigationController *navController;
UIViewController *firstViewController;
UIViewController *secondViewController;
then move to AppDelegate.m and instantiate each of those 4 items like this:
mainTabBar = [[UITabBarController alloc] init];
firstViewController = [[firstView alloc] init];
do that for both views
then if you want to set the title of either of the views, (this will be the title that shows up in the tab bar) do it as follows:
firstViewController.title = #"Welcome";
Then create the UINavigationController for the view that has a UITableView inside it like this:
navController = [[UINavigationController alloc] init];
[navController pushViewController:firstViewController animated:NO];
Now you have a UIViewController and a UINavigationController with a UIViewController inside it.
All thats left is to put your two tabs into the UITabBarController:
mainTabBar.viewControllers = [NSArray arrayWithObjects:firstViewController, secondViewController, nil];
then just put the tab bar controller on screen and you should be good to go:
[window addSubview:mainTabBar.view];
A couple things to remember:
make sure you release everything you called alloc on so your using good memory management.
Make sure you import all the files you intend on using in AppDelegate.h, it should look something like: #import "FirstView.h
Let me know in a comment if you have any questions
The document on Apple's developer's library portal, titled Combined View Controller Interfaces, explains how what you are after can be done both through the use of Interface Builder as well as programmatically. I recently followed the documentation on that page to create a part of an application that did entirely what you want to do.

can't add UIView from a UIViewController

I have some issue showing a view from a view controller. Basically, I have a UIViewController class, I programmatically created the view by overloading loadView method. Here is the code that I have:
UIView* view = controller.view;
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if(!window)
{
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
[window addSubview:view];
The view is not visible.
However, if I created a simple view based app by calling
[self presentModalViewController: controller animated: YES];
Everything works well.
I am new to the iOS View Programming. Anything I did was wrong?
Steve,
I'm not sure what you are trying to achieve here, but a typical iphone app will have a base UIViewController subclass, say a nav controller or tab controller and that controller's view is added as a subview of the UIWindow view in the appDelegate class.
You then instantiate further UIViewControllers or subclasses thereof and those views get added as subviews of the current view and so on.
Have you checked out the basic templates in XCode? These should show you how the appDelegate and the like works. You generally don't need to mess with UIWindow in other classes, in my experience.

Help Creating multiple views with view controllers

Ok here is what i am trying to do. In my root view controller I have the main view and then inside that view, i have three additional views. (note this is for the ipad).
Here is what I want to do. When the root view loads i want it to load the other three views as well and all have their own view controller.
Here is what I have attempted to far.
In my root controller xib I put in three view controllers and deleted their views. I then plugged into each controller view slot the views i have laid out within my root controller view. I also plugged in the view controller refrences with the ones i set up in rootcontroller.h
In my rootcontroller.m under the viewdidload i tried setting for example.
theViewController = [[ViewController alloc]
initWithNibName:#"AView" bundle:[NSBundle mainBundle]];
but to no avail that did not work
ViewControllers where desgined to use the entire screen, so this case should not happen. However, is very common that you need some complex user interactions inside your views (specially in iPad) to consider the use of a viewcontroller with some child viewcontrollers that encapsulate this logic as you explained above.
What I suggest you is to implement it by code. You could use something like:
-(void) viewDidLoad {
//Create view controllers
viewController1_ = [[ViewController alloc] initWithNibName:#"View1" andBundle:nil];
viewController2_ = [[ViewController alloc] initWithNibName:#"View1" andBundle:nil];
viewController3_ = [[ViewController alloc] initWithNibName:#"View1" andBundle:nil];
//Add the views to your main view
[self.view addSubview:viewController_1.view];
[self.view addSubview:viewController_2.view];
[self.view addSubview:viewController_3.view];
//TODO: Maybe set the appropiate frames?
}
Becareful with the event bypass. As you are embebing your secondary viewcontrollers into the main one, none of the standard events will be passed down to your child controlles (for example: viewWillAppear, shouldAutorotate,... will not be received by your child controllers). Remember to bypass them explecitly if you need them in your child viewcontrollers.
If your views are not correctly created using IB, check that you dont have problems with these event bypass problem.
Hope that helps!

Navigation & View Controller questions

I'm experimenting with ViewControllers & NavigationControllers in Interface Builder trying to get a better grasp of what's tied to what and why... I'm struggling with a scenario that has confused me. Hopefully someone can set me straight...
Say I start with your typical iPhone template View-Based Application and I display a view which is handled by view controller (viewController). Then after a certain event I'd like to replace that view with a "typical" Navigation-Based View (rootVC). I'd like to create as much as possible in IB. My questions have to do with how to show rootVC and remove all traces of the previous viewController as user will never need to return and where/how to wire in the navController in IB. Currently when it's time to show the rootVC I do the following in my viewController:
RootVC *rvc = [[RootVC alloc] initWithNibName:#"RootVC" bundle:nil];
[rvc.view setFrame:[[UIScreen mainScreen] applicationFrame]];
ViewTestAppDelegate *appDelegate = (ViewTestAppDelegate *)[[UIApplication sharedApplication] delegate];
self.rootVC = rvc;
[rvc release];
[appDelegate.viewController.view removeFromSuperview];
[appDelegate.window addSubview:rootVC.view];
[appDelegate.viewController release];
rootVC displays except viewController still has a retain count of 1?!?
Also, where should rootVC's navigationController be instantiated? Having started with the View-Based template the MainWindow.xib contains an object for the viewController (which has its own ViewController.xib) an appDelegate and a UIWindow. My RootVC.xib contains a UITableView. Do I need yet another intermediary view controller that will have another ApplicationDelegate object that I wire up to a UIWindow object and a UINavigationController? The View Controller that comes along with IB's Navigation Controller object would then be set to my RootVC class?
Sorry for the verbosity. It's difficult for me to explain. Because some objects in IB are proxies and some are "real" it's sometimes confusing (to me) when trying "new" things out what's required, where & when. Basically I want to know to go about setting up one view leading to another with no way back to first view. 2nd view basically becomes the "main" root spawning off in many directions...
I would recommend using the navigation-based iPhone application template and presenting your one-time view as a modal view on top of the root view.
I was able to figure it out by putting a reference to the viewController in the MainWindow nib and then autoreleasing the viewController after I added the navigationController & rootVC to the UIWindow. Learned another thing or two about IB along the way. Pretty powerful...