3

Using MonoTouch I add a LogonViewController to the Window and show it on FinishedLaunching:

        window = new UIWindow(UIScreen.MainScreen.Bounds);
        window.RootViewController = new LogonViewController();
        window.MakeKeyAndVisible();

In the LogonViewController, how do I add the main VC, called MainViewContoller and remove the LogonViewController? (This is the action that will happen once the user is logged in.)

Ian Vink
  • 66,960
  • 104
  • 341
  • 555

2 Answers2

6

Even if it's possible to replace the window.RootViewController, that's not how it's usually done. Most of the time, you define your RootViewController and handle your navigation, including login, from there. That's at least how I do it.

//AppDelegate.cs
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
    window = new UIWindow (UIScreen.MainScreen.Bounds);
    window.RootViewController = new MainViewController ();      
    window.MakeKeyAndVisible ();
    return true;
}

//MainViewController.cs
public override void ViewDidLoad ()
{
    base.ViewDidLoad ();
    if (not_logged_in)
        PresentViewController (new LoginViewController (), true, ()=>{});
}
Stephane Delcroix
  • 16,134
  • 5
  • 57
  • 85
5

For what its worth this is how I've done it before.

public static void swapRootView(UIViewController newView, UIViewAnimationOptions opt)
        {
            UIView.Transition(mainWindow, 0.5, opt, delegate{
                mainWindow.RootViewController = newView;

            },null);
        }

Then after login is ok, call that method with this option.

swapRootView(yourNewViewController, UIViewAnimationOptions.TransitionFlipFromRight);
Matt
  • 3,638
  • 2
  • 26
  • 33