【问题标题】:NavigationController is null after setting RootViewController in Xamarin.iOS在 Xamarin.iOS 中设置 RootViewController 后 NavigationController 为空
【发布时间】:2015-12-31 13:16:53
【问题描述】:

我在 Xamarin.iOS 中使用情节提要。 DashboardViewController(一个 UINavigationController)被设置为故事板中的初始视图。但是,在我的 AppDelegate 类中,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 为 null,应用程序崩溃。所以这个场景的基本流程是“将 RootViewController 设置为“ViewController”,以便用户可以登录 --> 当用户登录时,将 RootViewController 设置回 DashboardViewController 并呈现该视图 --> 单击一个按钮导航到另一个从 DashboardViewController 视图查看,但 DashboardViewController 中的 NavigationController 为空。

我做错了什么??任何帮助或建议将不胜感激(我为这个冗长的问题道歉)。

【问题讨论】:

    标签: c# xamarin xamarin.ios


    【解决方案1】:

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

    Window.RootViewController = rootViewController;
    

    我不得不说,

    Window.RootViewController = new UINavigationController(rootViewController);
    

    其他一切都保持不变。工作流程现在按预期工作。这是因为我试图设置为 RootViewController 的控制器不仅仅是一个 UIViewController,它是一个由 UINavigationController 包装的 UIViewController。在以下链接中也重申了这个答案

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

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

    【讨论】:

    • 这解决了我的问题!这个问题和答案在谷歌上相当落后,这绝对是正确的做法吗?谢谢大卫。
    • 很高兴有人发现它有用 :) 就像我在过去六个月中通过网络阅读的一样多,这就是实现它的方法,只需创建一个新的 UINavigationController,传入你的 UIViewController。我很早就了解到,即使使用情节提要,某些事情也必须在代码中完成,这就是其中之一。
    • 准确而准确的答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    相关资源
    最近更新 更多