如何在swift中使用模态视图?

roc*_*101 5 interface-builder modalviewcontroller ios swift

将帐户添加到Mail(在首选项中)时,您将获得模态视图,如下所示: 示例图像,来源:kb.xcentric.com/images/ipad12.jpg

我的问题是,我如何以编程方式复制这个?换句话说,如何在呈现视图上显示模态UIView?

这就是我所拥有的:

import UIKit


class ViewController: UIViewController {



@IBAction func addCard(sender: AnyObject) {
    var addContact : secondViewController = secondViewController()
    self.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal 
    self.modalPresentationStyle = .CurrentContext // Display on top of current UIView
    self.presentViewController(addContact, animated: true, completion: nil)
}
//Code goes on to do other unrelated things
Run Code Online (Sandbox Code Playgroud)

另外,我做了以下事情:

  1. 在"界面"构建器中创建了View Controller.
  2. 将BarButtonItem"添加联系人"连接到viewcontroller,通过按住Control键并单击,拖动到我想要显示的视图的viewcontroller,并在下拉列表中选择"modal".
  3. 将呈现的Viewcontroller的Class和StoryBoard UI设置为secondViewController.

预期的行为是,当按下UIBarButton"Add Contact"(@IBAction func addCard(sender: AnyObject)在上面的代码中成功触发 )时,secondViewController将呈现为主视图上方的模态视图.

当我运行上面的代码时,我收到错误 "Use of undeclared type secondViewController"

我究竟做错了什么?

注意:这是对早期问题的重新提问,其中我问了一个类似但略有不同的问题.我检查了meta,我认为它没关系 - 我不想让原始问题的答案无效,因为这是在问一些稍微不同的东西.另外,如果它有帮助,我发现了一些类似的问题 - 在obj C.我如何在swift中做到这一点?

Hid*_*oeg 8

试试这个 :)

@IBAction func addCard(sender: AnyObject) {
        self.modalTransitionStyle = UIModalTransitionStyle.coverVertical 
        // Cover Vertical is necessary for CurrentContext 
        self.modalPresentationStyle = .currentContext 
        // Display on top of    current UIView
        self.present(secondViewController(), animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)