Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Xcode 4.2 and Navigation Controller

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)

like image 353
cyclingIsBetter Avatar asked Dec 22 '11 16:12

cyclingIsBetter


People also ask

What is navigation Controller in IOS?

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.

How do I embed navigation controller in storyboard?

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 .


1 Answers

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.

like image 169
Jin Avatar answered Oct 25 '22 06:10

Jin