0

I'm using an archived object to determine which view needs to load on startup (or just opening the app from sleep) with boolean flags. Although my if statements evaluate correctly, the code within them does not.

Here is what's in 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
        }

And here's the console output:

Recalling Status Overseer from Archive...
User did finish application: false
Banks Did Respond: false
user did accept terms: true
Did Run

Notice that the bottom else if ran as supposed to but the view I see on startup is not LicenseViewNav as declared in the lines:

let vc = sb.instantiateViewControllerWithIdentifier("LicenseViewNav") as! UIViewController    
UIApplication.sharedApplication().keyWindow?.rootViewController = vc

If anyone knows what's going on, that'd be great. Thanks!

Edit: Current Problem seems to be that keyWindow evaluates to nil

Cole
  • 2,641
  • 1
  • 16
  • 34

2 Answers2

0

best way to set root view controller in swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

     var rootView: MyRootViewController = MyRootViewController()

     if let window = self.window{
            window.rootViewController = rootView
     }

     return true
}
Community
  • 1
  • 1
Mehul
  • 3,033
  • 23
  • 37
0

It was a simple fix by forcing the keyWindow to initialize. I just put this line above my if statement, it works now.

window?.makeKeyAndVisible()

Cole
  • 2,641
  • 1
  • 16
  • 34