iOS( swift ): Presenting View Controller Embedded in Navigation Controller

标签 ios swift uiviewcontroller uinavigationcontroller segue

我有一个嵌入在 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/

相关文章:

ios - Swift .insert 在数据库条目中

ios - 纵向视差滚动 Sprite 套件

ios - 如何在没有NavgationController的情况下打开UIViewControllers并保持状态

ios - UIView 和 UIViewController 设计模式

ios - 在 UIView 中从 UIViewController 快速调用组件

iOS : Open my App Specific Location Settings

ios - "insetGrouped"样式中单元格预览的圆角 (iOS 13)

swift - 如何将变量从 ViewController 类传递到其他结构?

swift - “CGImageSource”没有名为 'takeUnretainedValue' 的成员

ios - CollectionViewCell 匹配数据时无法正常工作