gpt4 book ai didi

ios - 在 subview Controller 之间切换

转载 作者:行者123 更新时间:2023-11-29 02:15:41 24 4
gpt4 key购买 nike

我有一个父级 UIViewController,它加载两个不同的子级 UIViewControllersAB。要加载 A,我会:

    [self addChildViewController:A];
[self.container addSubview:A.view];
[A didMoveToParentViewController:self];

然后当我想添加 B 时,我将使用相同的代码。我的问题是如何删除 A. 这就是我所需要的吗:

    [A removeFromParentViewController];

还是我还需要添加 [A.view removeFromSuperview];

最佳答案

在最简单的场景中,向容器 Controller 添加子级需要三个步骤:

  1. 在父级上调用 addChildViewController: 并将子级作为参数传递(例如,[self addChildViewController:childvc])。

  2. 将子 Controller 的 View 添加为 subview (例如,[self.view addSubview:childvc.view])。

  3. 在子级上调用 didMoveToParentViewController:,并将父级作为参数(例如,[childvc didMoveToParentViewController:self])。

要删除 subview Controller ,步骤几乎(但不完全)镜像:

  1. 在子级上调用 willMoveToParentViewController:,传递 nil 作为参数(例如,[childvc willMoveToParentViewController:nil])。

  2. 删除子 Controller 的 View (例如,[childvc.view removeFromSuperview])。

  3. 对子级调用removeFromParentViewController(例如,[childvc removeFromParentViewController])。

关于ios - 在 subview Controller 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28777207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com