如何以编程方式从AppDelegate设置初始ViewController?

时间:2016-12-23 11:38:04

标签: cocoa xamarin xamarin.mac

正如我的问题,我在这一点上陷入困​​境。实际上我的问题是,在我的示例中,我必须在应用程序启动时显示登录屏幕,如果用户成功登录他/她的凭据,他/她将重定向到主屏幕。如果用户打开应用程序第二次,我必须显示主屏幕,因为他已登录。

任何人都可以告诉我如何解决这个问题。

2 个答案:

答案 0 :(得分:2)

首先,您需要通过打开故事板,选择窗口控制器以及在属性检查器中取消选中“是初始控制器”来阻止cocoa加载初始控制器

然后是这样的:

    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);          
    }

使用FirstController和SecondController作为主故事板中两个NSWindowControllers的标识符。

答案 1 :(得分:0)

在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;
}
相关问题