简体   繁体   English

Swift-以编程方式加载情节提要

[英]Swift - Load storyboard programmatically

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // step 1. check the device
    var idiom = UIDevice.currentDevice().userInterfaceIdiom

    // step 2. take a storyboard variable
    var storyBoard:UIStoryboard? = nil

    // step 3. load appropriate storyboard file
    if idiom == UIUserInterfaceIdiom.Phone {
        storyBoard = UIStoryboard(name: "Main", bundle: nil)
    } else {
        storyBoard = UIStoryboard(name: "Main_iPad", bundle: nil)
    }

    // step 4. un-box storyboard to sb variable
    if let sb = storyBoard {

        // step 5. create new window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)

        // step 6. generates error :( 'Cannot assign to the result of this expression'
        self.window?.rootViewController?.storyboard = sb

        // step 7. make key window & visible
        window?.makeKeyAndVisible()
    }
    return true
}

I get error on step-6 ! 我在第6步中遇到错误! As I'm new to swift, I'm finding it bit difficult what to code here. 由于我是新手,所以我发现在此处编写代码有点困难。

The storyboard is just an XML file holding the information about your view controllers. 故事板只是一个XML文件,其中包含有关视图控制器的信息。 You should use it to instantiate view controllers (or maybe other controllers such as tab bar controllers or navigation controllers) to set as your application window's root view controller. 您应该使用它实例化视图控制器(或者可能是其他控制器,例如标签栏控制器或导航控制器)以设置为应用程序窗口的根视图控制器。

window.rootViewController = sb.instantiateInitialViewController() as MyWhateverController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM