0
votes

I am trying to accomplish what I think is a pretty common set of steps:

When my app starts, load a home controller in a navigation controller:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window.rootViewController = UINavigationController(rootViewController: HomeViewController())
    window.makeKeyAndVisible()
    self.window = window
    return true
}

When my app loads check if the user is logged in. If not, present a registration view controller:

func applicationDidBecomeActive(application: UIApplication) {
    if ref.loggedIn != nil {
        // user authenticated
        print(ref.userData)
    } else {
        // No user is signed in
        let registerViewController = RegisterViewController()
        if let navController = window?.rootViewController as? UINavigationController {
            navController.presentViewController(registerViewController, animated: true, completion: nil)
        }
    }
}

In my RegisterViewController, provide a button that switches to a LoginViewController:

@IBAction func login(sender: UIButton) {
        print("LOGIN")
        let loginViewController = LoginViewController()
        // How do I present the view controller here?
    }

My question is: how can I present the LoginViewController so that calling self.dismissViewControllerAnimated(false, completion: nil) will return to my HomeViewController?

Things I've tried:

  • If I call self.presentViewController from the RegisterViewController then dismissing returns back to the RegisterViewController instead of HomeViewController
  • If I try to get a reference to rootViewController via UIApplication.sharedApplication() and present the login controller on rootViewController then I get an error "Attempt to present ... on ... whose view is not in the window hierarchy!"

Thanks!

3

3 Answers

0
votes

It's obvious that you will get the error "Attempt to present ... on ... whose view is not in the window hierarchy!"

First you need to make the instance of ViewController using storyboard identifier.

Please try this :-

let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("viewControllerToBePresented") as! UIViewController //use your class name here to cast it.
self.presentViewController(vc, animated: true, completion: nil)

EDIT

In your case you can use protocol on register screen which will get implemented on home screen.

And in that implementation you can write code to dismiss register view and then present Login view.

0
votes

If you are trying to present a new view controller add that view controller in navigation controller and then present the navigation controller.. From the presented view controller you can dismiss to previous view controller. I hope this will work..

0
votes

In your case you can use protocol on register screen which will get implemented on home screen.

And in that implementation you can write code to dismiss register view and then present Login view.