1
votes

Currently i have: 1. Initial ViewController - AppStateViewController 2 .TabBarController - AuthorizedSessionViewController 3. ViewController - UnauthorizedSessionViewController

AppStateViewController Code - https://gist.github.com/nspavlo/de7a03f80e57694a12ba

If I call switchToAuthorizedSessionViewController() and switchToUnauthorizedSessionViewController() in viewDidLoad() AuthorizedSessionViewController appears instead of UnauthorizedSessionViewController.

My gol is to load right controller on didFinishLaunchingWithOptions and change it when appState is changed.

1

1 Answers

6
votes

Why you use switchToAuthorizedSessionViewController() in your switchToViewController() method:

func switchToViewController(identifier: String) {
    let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(identifier) as UIViewController
    self.navigationController?.setViewControllers([viewController], animated: false)
    **switchToAuthorizedSessionViewController()**
}

You will create an infinite loop with that, try this:

class AppStateViewController: UIViewController {
    var authorized: Bool = false {
        didSet {
            if authorized { 
                 switchToAuthorizedSessionViewController()
            } else {
                switchToUnauthorizedSessionViewController()
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.authorized = false
    }

    func switchToAuthorizedSessionViewController() {
        let sessionViewControllerID = "AuthorizedSessionViewController"
        switchToViewController(sessionViewControllerID)
    }

    func switchToUnauthorizedSessionViewController() {
        let sessionViewControllerID = "UnauthorizedSessionViewController"
        switchToViewController(sessionViewControllerID)
    }

    func switchToViewController(identifier: String) {
    let viewController =self.storyboard?.instantiateViewControllerWithIdentifier(identifier) as UIViewController
        self.navigationController?.setViewControllers([viewController], animated: false)
    }
}

with this you will be observing the property authorized and each time you change it, it will call the correct method.