【发布时间】: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