我正在学习 Swift,作为这个过程的一部分,我试图弄清楚这里到底发生了什么。我有一个自定义 Segue,我想在其中放置模态视图控制器以消除转换。 Objective-c 中以前的内容是:
UIViewController *sourceViewController = self.sourceViewController;
[sourceViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
self
是一个实例UIStoryboardSegue
。
我用 Swift 翻译了这个片段:
self.sourceViewController.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
从编译器中得到此错误:
'UIViewController?'没有名为的成员
'dismissViewControllerAnimated'
Now, 通过文档 https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/index.html#//apple_ref/occ/instp/UIViewController/presentingViewController, the presentingViewController
方法如下所示:
var presentingViewController: UIViewController? { get }
根据我对 Swift 语言文档的理解,?
应该解开该值(如果有)。在本例中是视图控制器。无法解释的事实是:如果我加一个双问号,它就会编译并运行:
self.sourceViewController.presentingViewController??.dismissViewControllerAnimated(true, completion: nil)
有人可以告诉我我错过了什么吗?那应该做什么呢?