huffy56
huffy56

Reputation: 3

How do I transfer back to a view controller from a view controller that is not connected to other view controllers?

I am working on an app for iOS that has a view controller not connected to other view controllers. Currently I am trying to get a button to transfer back to a login/signup view controller from the main view controller that is instantiated after the user has logged in. The view controller that is instantiated after the user has logged in is not connected to the other view controllers. The way I get to the unconnected view controller is with

func transitionToHome() { let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewControllers) as? homeViewController

view.window?.rootViewController = homeViewController
view.window?.makeKeyAndVisible()}

I am also including an image of the view controllers so that it is easier to understand how I have it set up photo of the view controllers

I have tried hooking up the "log out" button to transition back to the login/signup view controller but that causes a separate, smaller scene to be brought up that I can just swipe away and be back at the main view controller. I have also tried using the code above to transfer back to the login/sign up view controller, but that caused the same problem.

Upvotes: 0

Views: 48

Answers (2)

Dobster
Dobster

Reputation: 560

Although you can swap the rootViewController after app launch, it may just be simpler to arrange view controllers so they all link back to a single rootViewController.

If you want an initial login screen that is skipped on subsequent launches of the app, then you could make the login view controller the rootViewController and in viewDidAppear() jump straight to the main view controller when the login is not required. Then you can unwind the segue for "logout".

In the app delegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let loginViewController = UIStoryboard(name: "Login", bundle: nil).instantiateInitialViewController()
    self.window?.rootViewController = loginViewController
    self.window?.makeKeyAndVisible()
}

Then in LoginViewController:

func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if alreadyAuthenticated {
        performSegue(withIdentifier: SegueIdentifier.goToMain, sender: self)
    }
}

Other combinations like this are possible depending on your exact needs. There may be some benefit in that you can access the default presentation and dismiss animations, which would be much harder to implement if you are swapping the rootViewController.

Upvotes: 1

Manoj
Manoj

Reputation: 1069

Looks like you will need to reset the view.window?.rootViewController to login/signup view controller. Something like this

view.window?.rootViewController = LoginViewController()
view.window?.makeKeyAndVisible()}

Upvotes: 1

Related Questions