在 Xamarin.iOS 中设置 RootViewController 后 NavigationController 为空

NavigationController is null after setting RootViewController in Xamarin.iOS

我在 Xamarin.iOS 中使用故事板。 DashboardViewController(一个 UINavigationController)被设置为故事板中的初始视图。但是,在我的 AppDelegate class 中,FinishedLaunching 方法在应用程序启动时有条件地检查用户是否需要登录。如果是这样,它会将 "ViewController"(登录控制器)设置为 RootViewController,否则它将实例化故事板设置的初始视图控制器,即 "DashboardViewController"。这是 FinishedLaunching 方法的代码。

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
    {
        // Override point for customization after application launch.
        // If not required for your application you can safely delete this method

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

        var storyboard = UIStoryboard.FromName("Main", NSBundle.MainBundle);

        bool isLoggedOut = true;
        UIViewController rootViewController;

        if (isLoggedOut)
            rootViewController = (UIViewController)storyboard.InstantiateViewController("ViewController");
        else
            rootViewController = (UIViewController)storyboard.InstantiateInitialViewController();

        Window.RootViewController = rootViewController;
        Window.MakeKeyAndVisible();
    }

验证用户凭据后,以下代码将尝试再次将 RootViewController 设置回原始 "DashboardViewController" 并显示该视图。这是从登录控制器完成的。

    partial void LoginBtn_TouchUpInside (UIButton sender)
    {
        var appDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;
        appDelegate.SetRootViewController (dashboardViewController, true);
        this.PresentViewController (dashboardViewController, true, null);
    }

这是 AppDelegate 中 "SetRootViewController" 的代码:

    public void SetRootViewController()
    {
        Window = new UIWindow(UIScreen.MainScreen.Bounds);
        var storyboard = UIStoryboard.FromName("Main", NSBundle.MainBundle);

        bool isLoggedOut = false;
        UIViewController rootViewController;

        if (isLoggedOut)
            rootViewController = (UIViewController)storyboard.InstantiateViewController ("ViewController");
        else 
            rootViewController = (UIViewController)storyboard.InstantiateViewController (viewController);

        Window.RootViewController = rootViewController;
        Window.MakeKeyAndVisible();
    }

到目前为止这有效,但是如果我尝试将另一个视图添加到 DashboardViewController(它是一个 UINavigationController)中的堆栈,从使用以下代码的按钮单击说,

    partial void ProfileBtn_TouchUpInside (UIButton sender)
    {
        this.NavigationController.PushViewController (profileViewController, true);
    }

NavigationController 为空,应用程序崩溃。因此,此场景的基本流程是“将 RootViewController 设置为 "ViewController",以便用户可以登录 --> 当用户登录时,将 RootViewController 设置回 DashboardViewController 并呈现该视图 --> 单击一个按钮进行导航从 DashboardViewController 视图转到另一个视图,但 DashboardViewController 中的 NavigationController 为空。

我做错了什么??任何帮助或建议将不胜感激(对于这个冗长的问题,我深表歉意)。

我决定把这个问题留给那些可能遇到同样问题的人。我已经找到了我认为是解决方案的方法。在我的 AppDelegate "SetRootViewController" 方法中,不是说

Window.RootViewController = rootViewController;

我需要说的是,

Window.RootViewController = new UINavigationController(rootViewController);

其他一切保持不变。工作流现在按预期工作。这是因为我试图设置为 RootViewController 的控制器不仅仅是一个 UIViewController,它是由一个 UINavigationController 包裹的一个 UIViewController。这个答案也在下面link

这里重申

https://forums.xamarin.com/discussion/23424/how-can-i-use-this-navigationcontroller-on-my-view

我很抱歉回答我自己的问题,如果有人有更好的方法或发现有问题,请告诉我。希望这对将来的人有帮助。