gpt4 book ai didi

c# - 在 Xamarin.iOS 中设置 RootViewController 后 NavigationController 为 null

转载 作者:行者123 更新时间:2023-11-30 13:55:27 25 4
gpt4 key购买 nike

我在 Xamarin.iOS 中使用 Storyboard。 DashboardViewController(一个 UINavigationController)被设置为 Storyboard中的初始 View 。但是,在我的 AppDelegate 类中,FinishedLaunching 方法有条件地检查用户在应用程序启动时是否需要登录。如果是这样,它会将“ViewController”(登录 Controller )设置为 RootViewController,否则它将实例化 Storyboard设置的初始 View Controller ,即“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”并显示该 View 。这是从登录 Controller 完成的。

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

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

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

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

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

最佳答案

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

Window.RootViewController = rootViewController;

我需要说的是,

Window.RootViewController = new UINavigationController(rootViewController);

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

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

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

关于c# - 在 Xamarin.iOS 中设置 RootViewController 后 NavigationController 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34523404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com