kurrodu
kurrodu

Reputation: 2188

How to initialise View Controller in a Project with Two Storyboards iOS Swift

I am working on an App, which has main.storyboard I am able to initialise the required view controller on App load by selecting isInitialViewController as shown below.

enter image description here

I have now added another storyboard to my project, called Customer.Storyboard.

I have two questions.

  1. Where exactly in the code are we telling that the main.storyboard has to be loaded first ?

  2. How can I initialise a view from Customer.storyboard on App load ( by selecting is initial View Controller in Customer.Storyboard doesn't work, it loads a view from main.storyboard) ?

Upvotes: 0

Views: 596

Answers (3)

alegelos
alegelos

Reputation: 2614

This is a great step by step tutorial about this with best practices.

A clean way to get a Storyboard intense. Later on the tutorial you will find a clean way to get the ViewController instance.

enum AppStoryboard: String {
   case main = "Main"
   case preLogin = "PreLogin"
   case timeline = "Timeline"
   var instance : UIStoryboard {
       return UIStoryboard(name: rawValue, bundle: Bundle.main)
   }
}

// USAGE :

let storyboard = AppStoryboard.Main.instance

About your question: On the project Plist u set the first Storyboard. By default it's the main.

Upvotes: 2

interchen
interchen

Reputation: 57

You should know two concepts: "default interface for app" and "default scene(view controller) for storyboard".

"default interface for app" is set on Info.plist, see the image below
default interface for app

"default scene(view controller) for storyboard" is set by isInitialViewController

So if you want to launch MainViewController in Main.storyboard, you should set default interface as Main.storyboard

If you want to initial CustomViewController in Custom.storyboard, you can set isInitialViewController in CustomViewController as true.

Then you can get CustomViewController instance by UIStoryboard(name: "Custom", bundle: Bundle.main).instantiateInitialViewController()

And if you want to initial a ViewController which is not an initialViewController, you can get instance by UIStoryboard(name: "Custom", bundle: Bundle.main).instantiateViewController(withIdentifier: "yourViewController"). Before this you should set Storyboard ID in your storyboard. Storyboard ID in storyboard file

Upvotes: 1

Alec
Alec

Reputation: 9575

  1. In your Info.plist
  2. delete the main.storyboard loading from your plist and alter your didfinishwithlaunchingoptions func:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: 
    
    [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.main.bounds)
    let storyboard = UIStoryboard(name: "XXX", bundle: nil)
    
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")
    
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
    
    return true
    

    }

On the Plist u set the starting Storyboard to use. By default, it is main. **Note: XXX is used to represent the name of whichever new file you would like to use.

Upvotes: 2

Related Questions