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

时间:2014-07-19 12:17:23

标签: ios storyboard swift

我为每个iOS设备系列创建了一个包含三个不同Storyboard的应用。现在,当应用程序启动时,我不知道如何选择正确的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
}

有人可以在Swift中为我提供有效的解决方案吗?我只找到了ObjC的解决方案。

感谢。

1 个答案:

答案 0 :(得分:11)

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

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

它将打开ID为yourViewControllerId

的视图控制器

您需要为viewcontroller提供标识符。 您可以通过突出显示视图控制器然后为其指定标识符来执行此操作: 然后,将您的标识符放在StoryBoard ID中。 enter image description here

所以对你而言:

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
}