Swift - Load storyboard programmatically

感情迁移 提交于 2019-12-11 12:40:00

问题


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 ! As I'm new to swift, I'm finding it bit difficult what to code here.


回答1:


The storyboard is just an XML file holding the information about your view controllers. 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


来源:https://stackoverflow.com/questions/26148067/swift-load-storyboard-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!