我有一个嵌入在 UINavigationController
中的 UIViewController
(AVC
)。 AVC
(以模态方式呈现)转向另一个UIViewController
(BVC
)。在 BVC 内部,变量 self.presentingViewController 是可选的 NavigationController 类型,而不是我想要的 AVC 类型预计。
我必须将第一个 childViewControllers
向下转换为 AVC
,如下所示:
let pvc = self.presentingViewController
if let avc = pvc?.childViewControllers.first as? AVC {
// ...
}
为什么 self.presentingViewController
不像我预期的那样,即 AVC
?
非常感谢。
最佳答案
访问它
if let pvc = self.presentingViewController as? UINavigationController {
if let avc = pvc.viewControllers.first as? AVC {
// ...
}
}
//
<强> From Docs
When you present a view controller modally (either explicitly or implicitly) using the present(_:animated:completion:) method, the view controller that was presented has this property set to the view controller that presented it. If the view controller was not presented modally, but one of its ancestors was, this property contains the view controller that presented the ancestor. If neither the current view controller or any of its ancestors were presented modally, the value in this property is nil.
关于iOS( swift ): Presenting View Controller Embedded in Navigation Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50614048/