Nick Alexander
Nick Alexander

Reputation: 1643

Replacing current Storyboard with a new Storyboard in iOS

I'm building an app and I've decided to build my user interfaces in multiple Storyboard files. Each Storyboard is a group of similar actions. For example, LogInStoryboard contains views for logging in and for registering a user.

I've run into a bit of problem with navigating to a new Storyboard and dismissing all the previous view controllers to clean up after myself.

This is my dataflow:

1) App launches into a log in view. Root view controller is LogInViewController, which lives in LogInStoryboard.

2) User taps the register button to summon the modal RegistrationViewController, also lives in LogInStoryboard.

3) User completes registration and is automatically signed in.

At this point, the view controller stack is [LogInViewController, RegistrationViewController]. After registering, the user is automatically signed in so I want to navigate to their home screen, HomeViewController. However, HomeViewController lives inside another Storyboard HomeStoryboard.

I want to dismiss both LogInViewController and RegistrationViewController, and then I want to instantiate HomeViewController from HomeStoryboard and present it. This way I have a simple view controller stack and the LogInStoryboard view controllers can all be deallocated.

What would be the best way to achieve this sort of flow? Or should I even be so worried about those old view controllers?

Upvotes: 1

Views: 2085

Answers (3)

user8125593
user8125593

Reputation:

set your HomeViewController in appdelegate as navigationcontroller and set navigationcontroller as rootviewcontroller

NOTE: Both actions perform in appdelegate

Upvotes: 0

Josh Homann
Josh Homann

Reputation: 16327

Its very often the case that your login flow acts almost as an entirely separate app who's only function is to authenticate the user and write the user info to your database. In this case when you are done with the login instead of performing a segue what you want to do is replace the entire view hierarchy with HomeViewController. To do this you can simply call UIApplication.shared.keyWindow! = UIStoryboard(name: "HomeStoryboard", bundle: nil).instantiateInitialViewController() (assuming you made HomeViewController the initial view controller). Of course this has no animation and is kind of ugly so you way want to use UIView.transition(with:duration:options:animations:completion:) to animate the change instead. Example:

    UIView.transition(with: UIApplication.shared.keyWindow!, duration: 0.25, options: .transitionCrossDissolve,
                      animations: {
                        UIApplication.shared.keyWindow!.rootViewController = UIStoryboard(name: "HomeStoryboard", bundle: nil).instantiateInitialViewController()
    })

Upvotes: 4

Marceeelll
Marceeelll

Reputation: 704

Swift 4.0

I would start with the HomeViewController as the Initial View Controller. And from there create the LogInViewController.

if shouldLogin {
    let storyboard = UIStoryboard(name: "LogInStoryboard", bundle: nil)
    let loginVCtrl = storyboard.instantiateViewController(withIdentifier: "yourLogInViewControllerID")
    present(loginVCtrl, animated: true)
}

And after your user finished the Login/Registering process you can call:

dismiss(animated: true)

So your ViewController stack looks like:

  1. [HomeViewController]
  2. [HomeViewController, LogInViewController]
  3. [HomeViewController]

Upvotes: 1

Related Questions