如何通过Swift for iOS调用不同的故事板?

Fod*_*ddy 5 storyboard ios swift

我为每个iOS设备系列创建了一个带有三个不同故事板的应用程序.现在我不知道如何在应用程序启动时选择正确的Storyboard?我正在检查屏幕高度以识别不同的设备:

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
}
Run Code Online (Sandbox Code Playgroud)

有人可以在Swift中给我一个有效的解决方案吗?我只找到了ObjC的解决方案.

谢谢.

Bas*_*Bas 12

我想你想打开一个视图?如果是这样,这段代码将完成这项工作:

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

它将打开id为的视图控制器: yourViewControllerId

您需要为viewcontroller提供标识符.您可以通过突出显示视图控制器然后为其指定标识符来执行此操作:然后将标识符放在StoryBoard ID中. 在此输入图像描述

所以对你来说它将是:

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
}
Run Code Online (Sandbox Code Playgroud)