Create UINavigationController in other xib? - iphone

Hello, I have this code in my AppDelegate:
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
Now I want to create a XIB file in which I want to put a UINavigationController and add a UITableView. How do I create this through code without changing the delegate class?
I've tried this but it does not work:
PlacesTableViewController *obj = [[PlacesTableViewController alloc]init];
obj.title = #"Farmacie intorno a te";
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:obj];
[self.window addSubview:navC.view];

First, the "modern" way to set up your window is to use the window's rootViewController property:
window.rootViewController = someViewController;
The old way, where you add a view controller's view to the window as a subview, still works but the app will log a complaint about wanting the root view controller to be set up by the time the app is done launching.
Second, if you're going to replace the root view controller (in this case you're replacing your viewController with navC) using the old style, you'd want to remove the old view controller's view from the window. Your best bet is to just use the window's rootViewController property, since your code will be expected to use that going forward anyway.

Related

SubView did not load in the first launching of my application

I'm new to iPhone development. I tried to build a simple application with a window and a navigation controller as a sub-view of this window. The problem is this: the sub view did not load when I launch the application. I just have a windows with black screen. To load the view controller, I have to quit the application and launch it a second time, then I have my sub view with the navigation controller. I added a button directly in the window to make sure that the black screen is not a problem, but I saw the button at startup.
This is the code I have in my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
[self.window addSubview:_navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Do you have any solution for this problem?
Thank you.
You need to make sure that you have your view hierarchy setup. The window's rootViewController will be the UINavigationController. The UINavigationController controls a hierarchy of viewControllers, so when you instantiate it, you need to assign a rootViewController. Often times this is a subclass of a UITableView.
Because you are alloc/initing the window, I'm assuming that you do not have a XIB/NIB with the UINavigationController and an associated rootViewController like a UITableViewController. Also, rather than adding the view of your navigation controller, you need to assign the rootViewController, to the window. Since iOS4 this is the preferred way of doing things. See here as well. Try this code:
YourViewController *yourViewController = /* code for alloc/initing your viewController */
_navigationController=[[UINavigationController alloc] initWithRootViewController:yourViewController ]
self.window.rootViewController=_navigationController; /* instead of using [self.window addSubview: _navigationController.view] */
[self.window makeKeyAndVisible];
If you are using a XIB/NIB, then you need to make sure the _navigationController is wired up to the XIB file and has a subclass of a viewController wired up as it's rootViewController.
Good Luck

Loading a different XIB file after Application launches in TabBar Application Template

I'm creating an app where user needs to login first and only after that they can view anything in the app. To achieve that, I have created a new XIB file with the name 'AuthView'.
I know I need to put the code inside 'applicationDidfinishLaunching' method, but I don't know what code do I need to put inside it.
I'm developing the app using 'Tab Bar Application' template.
By default the end of the ApplicationDidFinishLaunchingWithOptions method looks like this:
[window addSubview:tabcontroller.view];
[window makeKeyAndVisible];
return YES;
Adding a viewController that appears over everything first is easy. First, add the viewController to your implementation (using the real name of your controller, obviously):
#import "InitialScreenViewController.h"
Then modify the end of your ApplicationDidFinishLaunchingWithOptions method by adding two lines as shown:
[window addSubview:tabcontroller.view];
initialScreenViewController = [[InitialScreenViewController alloc] init];
[window addSubview:initialScreenViewController.view];
[window makeKeyAndVisible];
return YES;
Once you've verified the login (or whatever you want to do with the initial screen) simply dismiss it within the initial screen viewController like this:
[self.parentViewController.view setHidden:YES];
This wil allow you to show it again later if need be, like if you add logout and re-login functionality.
As you're using the Tab bar application template, your UIApplicationDelegate file should have
a UITabBarController ivar named tabBarController.
You'll need to remove this ivar (and also remove it from your mainWindow XIB file).
Once this is done, go back to applicationDidfinishLaunching method and add the authentificate view in the windows like this :
MyAuthentificateViewController * viewController = [[[MyAuthentificateViewController alloc]initWithNibName:#"XIBFilename" bundle:nil] autorelease];
[windows addSubview:myAuthentificateViewController.view]];
Better you show your authenticate view first. In another view, place your tabbar controller.

iPhone Sdk: How to add a second UINavigationController?

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.

iPhone: create new "View-based Application" = no view controller?

I have created a new iPhone "View-based Application" in Xcode. I then added a new "UIViewController subclass" and checked the "with XIB for user interface. Now the issue I have is that after hooking up all the variables and message handlers, I cannot push the new controller onto the stack using the following code:
[self.navigationController pushViewController:self.cabinetController
animated:YES];
All the variables and views are hooked up correctly, so all that I can think of is that its the way I am doing it, by pushing it onto the "navigationController". Is there something I am missing here? (I am very new to iPhone and Apple programming in general, so its probably a very simple oversight).
I realise that not enough information has been supplied ... here is a link to the project. Please note that it is an educational exercise has some creatively names classes.
http://files.me.com/nippysaurus/4yqz8t
In your appDelegate create a UINavigationController instance variable and then use your existing viewController as the rootViewController of the navigation controller.
e.g. in pure code using a UITableViewController (you can use xibs as well which your template app probably does).
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create root view and navigation controller
UITableViewController *rootViewController = [[[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
// Not necessary if you're using xibs
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Add the nav controller's root view to the window
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
You need to change your view controller to a navigation controller, with its root view controller set as the current view controller.
If you examine your self.navigationController, you will realize it is nil. Messaging nil doesn't hurt, so no error message here.
Add another layer with a UINavigationController, and add your RandomShitViewController (nice name btw.) as its root view controller.
The navigation controller handles the push / pop part, your old controller manages its view.

TableView To Navigation Controller on the View-Based Application Project?

I built an application. On the one my views I used TableView. So now I want to change this Table view to a navigation controller.
1- How can I change UITable view to Navigation Controller.؟
The easiest way to achieve this is to create a new 'Navigation-based Application'. This will set up everything you need. By default this will set you up with simple RootViewController, if you want you can change this to be your TableViewController by editing the application delegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
All you need to do is copy your TableViewController into the new project and change the RootViewController to be yours
If you have started with one of the template project in XCode find the .xib file for your view and launch InterfaceBuilder by double clicking the .xib file. You can manipulate the view directly by dragging and dropping components as well as adding components from a palette.
I would walk through one of Apples tutorials and get a feel for working with InterfaceBuilder - this tutorial shows adding a view controller.
Thank you everybody . finally i figured out :D , just put this code :
UINavigationController *myNav=[[UINavigationController alloc] initWithRootViewController:[[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil]];
[self presentModalViewController:myNav animated:YES];