OGeek|极客世界-中国程序员成长平台

标题: ios - 替换父 View Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:07
标题: ios - 替换父 View Controller

我有以下具有父子关系的 View Controller 层次结构。

VC1 -- VC2 -- VC3 -- VC4 -- VC5

每个 View Controller 都是前任的 child 。

我用过代码-

   [parentVC addChildViewController:childVC];

   [childVC.view setFrame:parentVC.view.frame];

   [parentVC.view addSubview:childVC.view];

   [childVC didMoveToParentViewController:parentVC];

现在,我想将 VC1 替换为另一个 View Controller ,例如在 VC5 执行操作后此层次结构中的 VC0,即删除 VC2 后,我希望使用 VC0 而不是 VC1。如何做到这一点?



Best Answer-推荐答案


UINavigationController 提供了一个 -setViewControllers:animated:可用于修改导航堆栈的方法。它通常用于将应用程序恢复到用户离开应用程序时的状态。来自文档:

Use this method to update or replace the current view controller stack without pushing or popping each controller explicitly. In addition, this method lets you update the set of controllers without animating the changes, which might be appropriate at launch time when you want to return the navigation controller to a previous state.

也就是说,使用这种方法来替换堆栈中的 View Controller ,从 UI 的角度来看,这听起来像是一个糟糕的计划——这似乎可能会让用户感到困惑,他们完全希望父 Controller 不会变形为完全不同的东西。

关于ios - 替换父 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21240898/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4