我正在使用一个已归档的对象来确定哪个视图需要在启动时加载(或者只是从睡眠中打开应用程序),并使用布尔标志。虽然我的if语句正确评估,但其中的代码却没有。
这是didFinishLaunchingWithOptions中的内容:
// Register permission to throw notifications
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert | .Badge | .Sound, categories: nil))
var so = StatusOverseer.sharedOverseer
println("User did finish application: \(so.userDidFinishApplication)")
println("Banks Did Respond: \(so.banksDidRespond)")
println("user did accept terms: \(so.userDidAcceptTerms)")
// Decide whether or not to present the waiting view
if so.userDidFinishApplication && !so.banksDidRespond {
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewControllerWithIdentifier("submitWaiting") as! UIViewController
UIApplication.sharedApplication().keyWindow?.rootViewController = vc
}
else if so.banksDidRespond {
println("bip")
let sb = UIStoryboard(name: "Main", bundle: nil)
if so.notificationWasGood {
let vc = sb.instantiateViewControllerWithIdentifier("approved") as! UIViewController
UIApplication.sharedApplication().keyWindow?.rootViewController = vc
}
else {
let vc = sb.instantiateViewControllerWithIdentifier("notApproved") as! UIViewController
UIApplication.sharedApplication().keyWindow?.rootViewController = vc
}
}
else if so.userDidAcceptTerms {
println("Did Run")
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewControllerWithIdentifier("LicenseViewNav") as! UIViewController
UIApplication.sharedApplication().keyWindow?.rootViewController = vc
}
这是控制台输出:
Recalling Status Overseer from Archive...
User did finish application: false
Banks Did Respond: false
user did accept terms: true
Did Run
请注意底部else if
按原样运行,但我在启动时看到的视图不是行中声明的LicenseViewNav
:
let vc = sb.instantiateViewControllerWithIdentifier("LicenseViewNav") as! UIViewController
UIApplication.sharedApplication().keyWindow?.rootViewController = vc
如果有人知道发生了什么,那就太好了。谢谢!
编辑:当前问题似乎是keyWindow
评估为nil
答案 0 :(得分:0)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
var rootView: MyRootViewController = MyRootViewController()
if let window = self.window{
window.rootViewController = rootView
}
return true
}
答案 1 :(得分:0)
通过强制keyWindow初始化,这是一个简单的修复。我只是把这一行放在我的if语句之上,它现在有效。
window?.makeKeyAndVisible()