在swift中是新手,我正在尝试使用storyboard转到使用以下代码的LoginController
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
var type = UIUserNotificationType.Badge | UIUserNotificationType.Alert | UIUserNotificationType.Sound;
var setting = UIUserNotificationSettings(forTypes: type, categories: nil);
UIApplication.sharedApplication().registerUserNotificationSettings(setting);
UIApplication.sharedApplication().registerForRemoteNotifications();
var navCtrl = UINavigationController();
var isLoggedIn = NSUserDefaults.standardUserDefaults().boolForKey(USERDEFAULT_ISLOGGEDIN);
if(isLoggedIn)
{
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var mainController: CategoriesController = mainStoryboard.instantiateViewControllerWithIdentifier(SEGUE_CATEGORIES) as CategoriesController
navCtrl.pushViewController(mainController, animated: true);
}
else
{
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var mainController: LoginController = mainStoryboard.instantiateViewControllerWithIdentifier(SEGUE_LOGIN) as LoginController
navCtrl.pushViewController(mainController, animated: true);
}
self.window?.rootViewController = navCtrl;
self.window?.makeKeyAndVisible();
return true
}
然而它只是显示空白屏幕。将navController指定为rootViewController有什么问题?
答案 0 :(得分:1)
您尚未创建窗口,因此self.window
将为零。仅当您从xib或故事板启动应用程序时,系统才会创建窗口。如果你不使用这些,你必须自己创建它。
由于你使用?
有条件地打开了navCtrl的分配,并且对makeKeyAndVisible()的调用根本不会被解释。
在使用之前简单地创建一个UIWindow:
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// ...
self.window!.rootViewController = navCtrl;
self.window!.makeKeyAndVisible();
return true
}
我建议将window
与!
的所有来电解包。没有窗口应该是一个错误。
答案 1 :(得分:0)
试试这种方式。我认为在设置其根ViewController之前,你无法推送UINavigationController。
var navCtrl
let isLoggedIn = NSUserDefaults.standardUserDefaults().boolForKey(USERDEFAULT_ISLOGGEDIN);
if (isLoggedIn)
{
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var mainController: CategoriesController = mainStoryboard.instantiateViewControllerWithIdentifier(SEGUE_CATEGORIES) as CategoriesController
navCtrl = UINavigationController(rootViewController:mainController)
}
else
{
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var mainController: LoginController = mainStoryboard.instantiateViewControllerWithIdentifier(SEGUE_LOGIN) as LoginController
navCtrl = UINavigationController(rootViewController:mainController)
}