ios 在 Swift 中以编程方式呈现模态视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33683012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:13:32  来源:igfitidea点击:

Presenting Modal View Controller programmatically in Swift

iosswiftmodal-dialog

提问by user3188157

In the main screen of my iOS application I have a table view populated with a list of users from a web service, what I want to do is to show the table, and over it a modal view controller. I have everything set up, but I don't know how to present my "ModalVC" programatically from the main screen, and to do it after the table was populated? The code below is for presenting a normal view controller programmatically.

在我的 iOS 应用程序的主屏幕中,我有一个表格视图,其中填充了来自 Web 服务的用户列表,我想要做的是显示表格,并在它上面显示一个模态视图控制器。我已设置好所有内容,但我不知道如何从主屏幕以编程方式显示我的“ModalVC”,以及如何在填充表格后执行此操作?下面的代码用于以编程方式呈现一个普通的视图控制器。

let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("ModalVC")
self.showViewController(vc as! UIViewController, sender: vc)

回答by Lukas

Presenting a view controller of class name ModalVC programmatically

以编程方式呈现类名 ModalVC 的视图控制器

let modalVC = ModalVC.instantiateFromStoryboard(self.storyboard!)
self.presentViewController(modalVC, animated: true, completion: nil)

Swift 3:

斯威夫特 3:

let modalVC = ModalVC.instantiateFromStoryboard(self.storyboard!)
self.present(modalVC, animated: true, completion: nil)