cyclingIsBetter
cyclingIsBetter

Reputation: 17591

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)

Upvotes: 5

Views: 6327

Answers (2)

Jin
Jin

Reputation: 128

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.

Upvotes: 5

lifemoveson
lifemoveson

Reputation: 1691

You have to create UINavigationController class in your project and attach in your delegate class meaning define an IBOutLet UINavigationController class in your application delegate class and define it in your delegate class. In your Interface Builder, connect the IBOutLet to the delegate class.

Upvotes: 0

Related Questions