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

时间:2014-08-09 22:17:39

标签: ios swift interface-builder modalviewcontroller

向Mail添加帐户(在首选项中)时,您会看到一个模态视图,如下所示: Example Image, source: 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

另外,我做了以下事情:

  1. 在“界面”构建器中创建了“视图控制器”。
  2. 将BarButtonItem“添加联系人”连接到viewcontroller,通过按住Control键并单击,拖动到我想要显示的视图的viewcontroller,然后在下拉列表中选择“modal”。
  3. 将呈现的Viewcontroller的Class和StoryBoard UI设置为secondViewController。
  4. 预期的行为是,当按下UIBarButton“Add Contact”(在上面的代码中成功触发@IBAction func addCard(sender: AnyObject))时,secondViewController将显示为主视图上方的模态视图。

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

    我做错了什么?

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

1 个答案:

答案 0 :(得分: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)
}