Switch between UIViewController and UINavigationController

Discussion in 'iOS Programming' started by edcruise, Mar 6, 2011.

  1. edcruise macrumors newbie

    Mar 6, 2011
    Hi at all,
    i've a question about UIViewController and UINavigationController.

    I'm working on an application in which I have a login view (through UIViewController) and other 3 view stacked on UINavigationController.

    I want to distinguish between LoginView and the other view, then I adopted this solution...mainly because if I use IUNavigationController, after login I can go back using back button from UINavigationBar....I dont want that it happens.

    My solution is to create a UIViewController for LoginView....then if login is ok, create UINavigationController and add it to subview. In second access, if I found that user already login, go directly to UINavigationController.
    But I have wrong view loaded...and the application gets an error.

    here that I have:

    app delegate
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
        [self.window addSubview:loginViewController.view];
        [self.window makeKeyAndVisible];	
        return YES;
    - (void)viewDidLoad {
        [super viewDidLoad];
        if (!logged) {
    	// do login process 
        HomeController *hController = [[HomeController alloc] initWithNibName:@"HomeView" bundle:nil];		
        UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:hController];
        [hController release];
        [self.view addSubview: nc.view];
        [self presentModalViewController:nc animated:YES];
        [nc release];
    On starting app first is loaded LoginViewController (UIViewController), it decides if user is logged in or not...then create UINavigationController with LoginViewController as root controller. But I get some displaying error, NavigationController on top of LoginController but all is blocked.

    Where am I wrong ? Is it not possible to call programmatically a UINavigationController from UIViewController after loaded ?

    Thanks in advance.
  2. cwalcott macrumors newbie

    Mar 6, 2011
    If you want to replace the login view with the UINavigationController, then you should remove it from the window, then add the nav controller.

    UIWindow *mainWindow = self.view.window;
    [self.view removeFromSuperview];
    [mainWindow addSubview:nc.view];

Share This Page