从嵌入式子UIViewController中获取Parent ViewController

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从嵌入式子UIViewController中获取Parent ViewController相关的知识,希望对你有一定的参考价值。

如何从使用Container View嵌入其中的控制器中获取对父UIViewController的引用?

我想从Child中访问Parent。

答案

在容器视图中添加视图控制器时,它将添加为子视图控制器

你可以按照以下方式访问它

  if let yourVC:YourViewController = self.childViewControllers.first(where: {$0 is yourVC:YourViewController}) as? YourViewController {
        // Here you got it 

    }

您可以使用viewController的parent属性访问Parent

  if let parent = self.navigationController?.parent as? ParentControllerType {

     // Do what you want with the parent.

   } 

看到这个https://developer.apple.com/documentation/uikit/uiviewcontroller/1621362-parent

希望它有所帮助

以上是关于从嵌入式子UIViewController中获取Parent ViewController的主要内容,如果未能解决你的问题,请参考以下文章

从 swiftUI 中的一个嵌入式 UIViewController 导航到 swift UI 中的另一个嵌入式 UIViewcontroller

ViewController 作为子视图

嵌入在 UIViewController 中的 UIHostingController - 如何从外部更新 @State?

无法从主 UIViewController 访问嵌入式 UICollectionView

将信息从 UINavigationController 传递到嵌入式 UIViewController

在 UIViewController 中嵌入 UIContainerView 作为屏幕的一部分