In XCode 4.2 when I select "new project" and also select "single view application" but now I want to add a navigation controller. What can I do in Xcode 4.2 to do it? (without storyboard)
A navigation controller is a container view controller that manages one or more child view controllers in a navigation interface. In this type of interface, only one child view controller is visible at a time.
In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .
Unless you are adding the UINavigationController to another UIViewController that is utilized for a different method of navigation, i.e. UISplitViewController or UITabBarController, I would recommend adding the UINavigationController to your application window in the AppDelegate then add the UIViewController that has your view in it.
If you are adding the UINavigationController as your main UIViewController, you can easily do this programmatically in the following method in the AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
The code I would add is:
UINavigationController *navcon = [[UINavigationController alloc] init];
[navcon pushViewController:self.viewController animated:NO];
self.window.rootViewController = navcon;
Now, in your AppDelegate.m it should look like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
}
else
{
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
}
UINavigationController *navcon = [[UINavigationController alloc] init];
[navcon pushViewController:self.viewController animated:NO];
self.window.rootViewController = navcon;
[self.window makeKeyAndVisible];
return YES;
}
You can further learn how to utilize the UINavigationController by checking out the UINavigationController Apple Documentation and their example projects, which you can download from the same documentation page. The example projects will help you grasp the various ways you can utilize the UINavigationController.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With