1

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 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.