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.