简体   繁体   English

如何从故事板以编程方式加载 UIViewController?

[英]How to load UIViewController programmatically from storyboard?

I created a UIViewController in my Main.storyboard with a few buttons and labels.我在 Main.storyboard 中创建了一个 UIViewController ,带有几个按钮和标签。 I'm trying to switch to that view controller using self.presentViewController but it will not load the view from storyboard.我正在尝试使用 self.presentViewController 切换到该视图控制器,但它不会从故事板加载视图。 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)

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.它不会根据您在 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.相反,您应该使用它,其中“SomeID”是您在 Interface Builder 中分配给视图控制器的故事板 ID。

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

You can assign a storyboard ID to your view controller in Interface Builder's identity inspector.您可以在 Interface Builder 的身份检查器中为您的视图控制器分配故事板 ID。

在此处输入图片说明

Full Swift 3 code including instantiation of Storyboard:完整的 Swift 3 代码,包括 Storyboard 的实例化:

let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)

if let mainViewController = storyboard.instantiateInitialViewController() {
    present(mainViewController, animated: false, completion: nil)
}

在此处输入图片说明

SWIFT 5.2斯威夫特 5.2

first, you should define a storyboardID for viewcontroller and after use this code首先,您应该为 viewcontroller 定义一个 storyboardID,然后使用此代码

 let storyboard = UIStoryboard(name: "Main", bundle: nil) // type storyboard name instead of Main
 if let myViewController = storyboard.instantiateViewController(withIdentifier: "myViewControllerID") as? CourseDetailVC {
       present(myViewController, animated: true, completion: nil)
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用故事板中的视图以编程方式创建UIViewController - How to Create a UIViewController programmatically with view from storyboard 如何以编程方式从UINavigationController(storyboard)到UIViewController - How to segue from UINavigationController (storyboard) to UIViewController programmatically 如何在不使用Swift中的Storyboard的情况下以编程方式加载UIViewController - How to load a UIViewController programmatically without using Storyboard in Swift 如何在故事板上创建一个Segue,从编程注册的UICollectionViewCell到新的UIViewController? - How to create a Segue on the Storyboard, from a programmatically registered UICollectionViewCell to a new UIViewController? 在Storyboard中以编程方式访问UIViewController - Accessing UIViewController programmatically in Storyboard 从情节提要UIViewController将视图加载到外部显示 - Load view from storyboard UIViewController to external display 如何使用共享的故事板设计加载正确的UIViewController? - How load the correct UIViewController with a shared storyboard design? 如何从类中以编程方式加载故事板? - How can I load storyboard programmatically from class? 在带有情节提要的项目中以编程方式创建UIViewController - Programmatically create a UIViewController in a project with a storyboard 如何以编程方式设置过渡样式并连接到故事板 UIViewController - How to set transition style programmatically and connect to storyboard UIViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM