How to call different Storyboards via Swift for iOS?

19,701

I guess you want to open a view? If so this code will do the job:

var mainView: UIStoryboard!
mainView = UIStoryboard(name: "vcLogin", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("iPhone5") as UIViewController
self.window!.rootViewController = viewcontroller

It will open the view controller with id: yourViewControllerId

You need to give your viewcontroller an identifier. You do that by highlighting your view controller and then give it a identifier: You then put your identifier in StoryBoard ID. enter image description here

So for you it will be:

if screenHeight == 480 {
  deviceFamily = "iPhoneOriginal"
  // Load Storyboard with name: iPhone4
  var mainView: UIStoryboard!
  mainView = UIStoryboard(name: "vcLogin", bundle: nil)
  let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("iPhone4") as UIViewController
  self.window!.rootViewController = viewcontroller
}
Share:
19,701
Foddy
Author by

Foddy

Updated on July 18, 2022

Comments

  • Foddy
    Foddy almost 2 years

    I created an app with three different Storyboards for each iOS device family. Now I don't know how to choose the right Storyboard when the app starts? I am checking the screen height to recognize the different devices:

    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
        // Check Device Family
        var bounds: CGRect = UIScreen.mainScreen().bounds
        var screenHeight: NSNumber = bounds.size.height
        var deviceFamily: String
        if screenHeight == 480 {
            deviceFamily = "iPhoneOriginal"
            // Load Storyboard with name: iPhone4
        } else if screenHeight == 568 {
            deviceFamily = "iPhone5Higher"
            // Load Storyboard with name: iPhone5
        } else {
            deviceFamily = "iPad"
            // Load Storyboard with name: iPad
        }
    
        return true
    }
    

    Can somebody give me a working solution in Swift? I only found solutions for ObjC.

    Thanks.

  • Foddy
    Foddy almost 10 years
    Hi! Thanks for your answer ;) But that was not really what I searched for. I created 3 different Storybaord files. Now I want to load a completely different Storyboard file when the app starts and based on the screen height...?! I tried your solution but I get some erros: i.minus.com/jKYZ16oT4aUTd.png
  • Foddy
    Foddy almost 10 years
    Does it mean (with your solution) that I need to put all my Views etc. in one Storyboard (from the iPad and iPhone...) and that call the first View based on its id?
  • Bas
    Bas almost 10 years
    Yes! I thought you had 1 storyboard with 3 different ViewControllers in it.
  • Foddy
    Foddy almost 10 years
    This is what I am searching for but in Swift: link
  • gutte
    gutte about 7 years
    Don't forget to dismiss the previous rootViewController