Search code examples
iphonexcodeiosuiviewcontrolleruiwindow

Change UIWindow's presented view from UIViewController method


I have a simple application with two subclasses of UIViewController.

I want to change the view being displayed by my application's UIWindow by calling a method from one of my UIViewController subclasses.

Essentially I'm just messing around and I'm trying to construct a simple test application with a login screen, that after a user enters credentials a main view is presented. I'm not too familiar with the window and view mechanisms of iOS programming, and I'm currently reading http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html#//apple_ref/doc/uid/TP40009503-CH2-SW1 and attempting to learn a bit about it.


Solution

  • If this is for the purpose of a login screen, you should add the main view controller to window directly, and add the login view controller as a modal view inside the main view controller.

    Inside applicationDidFinishLaunching...

    MainViewController *mainViewController = [[MainView....... // instantiate UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:mainViewController];
    [mainViewController release]; 
    [window addSubview:navController.view];
    

    Inside MainViewController

    -(void)viewWillAppear:(BOOL)animated
    {
        LoginViewController *loginVC = .... //instantiate
        [self.navigationController presentModalViewController:loginVC  animated:NO];
        [loginVC release];
    }
    

    if login successful,

    [self dismissModalViewControllerAnimated:YES];