Swift - 以编程方式加载故事板

时间:2014-10-01 18:27:44

标签: ios dynamic swift storyboard

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
}

我在第6步得到错误!由于我是swift的新手,我发现在这里编码很困难。

1 个答案:

答案 0 :(得分:5)

故事板只是一个XML文件,其中包含有关视图控制器的信息。您应该使用它来实例化视图控制器(或者可能是其他控制器,如标签栏控制器或导航控制器),以设置为应用程序窗口的根视图控制器。

window.rootViewController = sb.instantiateInitialViewController() as MyWhateverController
相关问题