Swift - 以编程方式加载故事板 [英] Swift - Load storyboard programatically

查看:132
本文介绍了Swift - 以编程方式加载故事板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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中收到错误!因为我刚刚开始,我发现在这里编码有点困难。

I get error on step-6 ! As I'm new to swift, I'm finding it bit difficult what to code here.

推荐答案

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

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

这篇关于Swift - 以编程方式加载故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆