如何从故事板以编程方式加载 UIViewController? [英] How to load UIViewController programmatically from storyboard?

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

问题描述

我在 Main.storyboard 中创建了一个 UIViewController,并带有一些按钮和标签.我正在尝试使用 self.presentViewController 切换到该视图控制器,但它不会从故事板加载视图.默认情况下,它只会加载一个空白的黑屏.知道如何从我在故事板中创建的内容加载视图吗?

I created a UIViewController in my Main.storyboard with a few buttons and labels. I'm trying to switch to that view controller using self.presentViewController but it will not load the view from storyboard. It will only load a blank black screen by default. Any idea on how to load the view from what i've created in storyboard?

self.presentViewController(ResultViewController(), animated: true, completion: nil)

推荐答案

您这样做的方式只是创建了视图控制器的新实例.它不会根据您在 Interface Builder 中定义的原型创建一个.相反,您应该使用它,其中SomeID"是您在 Interface Builder 中分配给视图控制器的故事板 ID.

The way you're doing this just creates a new instance of your view controller. It does not create one from the prototype you've defined in Interface Builder. Instead, you should be using this, where "SomeID" is a storyboard ID that you've assigned to your view controller in Interface Builder.

if let resultController = storyboard!.instantiateViewControllerWithIdentifier("SomeID") as? ResultViewController {
    presentViewController(resultController, animated: true, completion: nil)
}

您可以在 Interface Builder 的身份检查器中为您的视图控制器分配故事板 ID.

You can assign a storyboard ID to your view controller in Interface Builder's identity inspector.

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

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