如何通过Swift for iOS调用不同的故事板? [英] How to call different Storyboards via Swift for iOS?
问题描述
我为每个iOS设备系列创建了一个包含三个不同Storyboard的应用。现在我不知道如何在应用程序启动时选择正确的Storyboard?我正在检查屏幕高度以识别不同的设备:
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
}
有人可以在Swift中为我提供有效的解决方案吗?我只找到了ObjC的解决方案。
Can somebody give me a working solution in Swift? I only found solutions for ObjC.
谢谢。
推荐答案
我猜你要打开一个视图?如果是这样,这段代码将完成这项工作:
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
它将打开视图控制器,其ID为: yourViewControllerId
It will open the view controller with id: yourViewControllerId
您需要为viewcontroller提供标识符。
您可以通过突出显示视图控制器然后为其指定标识符来实现:
然后将标识符放在StoryBoard ID中。
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.
所以对你来说它将是:
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
}
这篇关于如何通过Swift for iOS调用不同的故事板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!