如何从storyboard以编程方式加载UIViewController? [英] How to load UIViewController programmatically from storyboard?
问题描述
我在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.
这篇关于如何从storyboard以编程方式加载UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!