phil swenson
phil swenson

Reputation: 8904

How to set up Navigation Controller -> Root View in code (iPhone SDK)

I need the ability to bypass the default RootViewController you get when you pick a navigation controller project type in XCode. I need this because I want to go down a different path depending on whether the app has been configured (sign up/login screens if not). Could someone point me to an example where in the AppDelegate the NavigationController is hooked to another controller (in this case SignupController) via code?

Here is what I have, but it doesn't let me change the title. And in the MainWindow.xib, it's still tied into the default RootViewController.

(void)applicationDidFinishLaunching:(UIApplication *)application {    
    [[UIApplication sharedApplication] 
    // if no config, load up the SignupController

    SignupController* signupController = [[SignupController alloc] initWithNibName:@"SignupController" bundle:nil];
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];

Upvotes: 1

Views: 6399

Answers (1)

bentford
bentford

Reputation: 33416

You can do something like this:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    if( hasConfig ) {
        [window addSubview:[navigationController view]];
    } else {
        SignupController *signupController = [[SignupController alloc] initWithNibName:@"SignupController" bundle:nil];
        [window addSubview:signupController.view];
        [signupController release];
    }
    [window makeKeyAndVisible];

It becomes awkward to switch back to your navigation controller once sign in is complete. (Assuming you are showing a sign in screen.)

Why not use a modalviewcontroller?

In your RootViewController.m:

- (void)viewDidAppear {
   [super viewDidAppear];

    if( notLoggedIn ) {
        SignupController *signupController = [[SignupController alloc] initWithNibName:@"SignupController" bundle:nil];

        [self presentModalViewController: signupController animated:YES];
        [signupController release];
    }
}

SignupController.m

- (void)didSignInOk {
    //this will dismiss the sign in screen
    [self.parentViewController dismissModalViewController];
}

Upvotes: 2

Related Questions