0

I know this is probably an easy task but I can't get my head around how to solve it. I'm not using a NavigationController by the way. Basically I have 3 view controllers in my app:

  • -LoginVC (this has a register button. when tapped, it goes to the SignupVC)
  • -SignupVC (if user signs up, it will push to the MainAppVC)
  • -MainAppVC (has a logout button)

For the transitions, I use the method: present(viewController, animated:, completion:)

When the user logs in via the LoginVC, he'll be presented the MainAppVC as expected. When he logs out, I'll dismiss the current VC (which is the MainAppVC) to send him back to the LoginVC.

Now here is the case where I have questions about. When the user does not have an account and signs up, this is the VCs he will pass through (LoginVC > SignupVC > MainAppVC). Once he registers successfully, he'll be presented the MainAppVC. Now once he logs out, he'll be transitioned from the MainAppVC to the SignupVC because I used the same dismiss method.

What I want to do is to send the user back to the LoginVC from the MainAppVC. How do I accomplish that without using a navigation controller in my project? How do popular apps (Facebook, Twitter, Instagram, etc) handle this in their apps?

One way I can think of is to perform a segue from 3rd to 1st VC but I think that's a dirty way since it just adds to the stack, instead of popping it off which is a potential issue.

aresz
  • 2,499
  • 6
  • 27
  • 49
  • [this](http://stackoverflow.com/a/15839298/1219956) might be what you need – Fonix Oct 10 '16 at 06:41
  • instead of present you can set root view controller is better option – Prashant Tukadiya Oct 10 '16 at 06:46
  • Thank you both for the comments. I will try both methods. @Fonix I saw that post too .By any chance, if I unwind from the 3rd to the 1st VC, would that also pop the 2nd VC? I want to make sure it's popped and not hanging around in memory. – aresz Oct 10 '16 at 07:02
  • yes it can pop multiple VC's so will pop the 2nd – Fonix Oct 10 '16 at 07:09

4 Answers4

2

There are three solutions.
1.Use window root view controller.Set root view controller between login and main view controller.

let loginSb = UIStoryboard(name: "Login", bundle: nil)
let loginVc = loginSb.instantiateInitialViewController()
window!.rootViewController = loginVc

2.Dismiss the sign up view controller the first time you present main view controller.And present the main view controller from login view controller.
3.Try to dismiss the sign up view controller in the completion block when you dismiss the main view controller.
I think the first one is best.

Lumialxk
  • 5,652
  • 6
  • 21
  • 46
2

If are forced to NOT use a rootViewController (with UINavigationController), you should check in MainAppVC which one is the previous VC (Pseudocode in the example) and do:

Note: Swift 3 Code.

// If previous VC is LoginVC
dismiss(animated: true)

// else if previous VC is SignupVC
// here's what you want
presentingViewController?.presentingViewController?.dismiss(animated: true)

Hope that helped.

Ahmad F
  • 26,570
  • 13
  • 76
  • 124
1

Another option is to use unwind segues.

In the ViewController that you want to go back to, implement a method as follows

@IBAction func unwindToLogin(segue: UIStoryboardSegue) {
}

Then in Storyboard, from the last ViewController right click and drag from the button (for example) to Exit and choose unwindToLoginWithSegue.

enter image description here

Note that you can also do this programatically. Here is a good tutorial.

Carien van Zyl
  • 2,705
  • 19
  • 27
  • Thanks for the info. I've tested this and it works. I've tested all the other answers and they work as well. But this method is the most convenient in terms of the setup of my project – aresz Oct 11 '16 at 06:07
1

Best approach is : Dismiss SignUpVC once User Register successfully, Then from LoginVC present MainVC.

In SignupVC :

self.dismissViewControllerAnimated(true, completion: {
                self.LoginVC!.present(MainVC, animated: true, completion:nil)

    })

This approach is used mostly.