How to set initial ViewController from AppDelegate programmatically?

2k views Asked by At

As my question I got stuck in this point. Actually my problem is, in my sample I have to show login screen on start of app if user logged with his/her credentials successfully he/her will redirect to Home screen. If user open app second time I have to show Home screen as he already logged in.

How do I solve this?

2

There are 2 answers

1
Chris Hamons On BEST ANSWER

First you need to prevent cocoa from loading an initial controller by opening your storyboard, selecting your window controller, and in the attributes inspector unchecking "Is Initial Controller"

Then something like this:

    NSWindowController controller;
    public override void DidFinishLaunching (NSNotification notification)
    {
        var storyboard = NSStoryboard.FromName ("Main", null);
        if (true)
            controller = (NSWindowController)storyboard.InstantiateControllerWithIdentifier ("FirstController");
        else
            controller = (NSWindowController)storyboard.InstantiateControllerWithIdentifier ("SecondController");
        controller.Window.MakeKeyAndOrderFront (this);          
    }

with FirstController and SecondController being the identifiers of two NSWindowControllers in your main storyboard.

1
Yuri S On

In AppDelegate:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{

        window = new UIWindow(UIScreen.MainScreen.Bounds);

        // If you have defined a root view controller, set it here:
    if(LoggedIn)
                window.RootViewController = new MainController();
    else
                window.RootViewController = new LoginController();

        // make the window visible
        window.MakeKeyAndVisible();


    return true;
}