ios - 呈现新的 View Controller 并无形地关闭当前呈现的 View Controller 链

标签 ios objective-c iphone uiviewcontroller

所以我有一个 View Controller 链(通常是 2 - 3 个),其中第一个是 Root View Controller ,下一个是用 presentViewController:animated: 呈现的。

那是应用程序的入职部分,当用户最终到达我需要显示主 UI 的部分时,我想在显示之前关闭所有入职 View Controller ( Root View Controller 除外)主 UI View Controller ,它是一个 UITabBarController。 我想这样做的原因是——我不再需要它们,我不想将那些 View Controller 保留在内存中。

现在的问题是 - 我希望这看起来像我只是在展示 UITabBarController,我不希望用户看到之前的 View Controller 被关闭。

我尝试的第一件事:

  1. 在 Root View Controller 上调用 dismissViewControllerAnimated:completion: 并设置 animated: NO
  2. 完成时使用 animated: YES 执行 presentViewController:animated:completion: 以显示我的 UITabBarController

可悲的是,在显示呈现我的 UITabBarController 的动画之前闪现了 Root View Controller 。

我尝试过的复杂解决方案:

  1. 拍摄主窗口的快照
  2. 将快照添加为 Root View Controller View 的 subview
  3. 在 Root View 上调用 dismissViewControllerAnimated:completion: 带有animated: NO
  4. 的 Controller
  5. 完成时使用 animated: YES 执行 presentViewController:animated:completion: 以显示我的 UITabBarController
  6. presentViewController 完成后,从 Root View Controller 的 View 中移除快照 View

这是执行此操作的代码。它有点复杂,因为它还处理弹出 Root View Controller 的 subview Controller ,因为 Root View Controller 是 UINavigationController:

- (void)popNavToRootAndPresentViewController:(UIViewController *)viewController
{
    UINavigationController *rootVC = (UINavigationController *)self.view.window.rootViewController;

    // if we have a chain of presented view controllers over our nav vc, then we need to dismiss them
    if (rootVC.presentedViewController) {
        // we need a snapshot view because even if dismissing without animation, root view controller will flash for a fraction of a second.
        __block UIView *snapShotView = [self.view.window snapshotViewAfterScreenUpdates:NO];

        [rootVC.view addSubview:snapShotView];

        // hide the currently presented view controller chain without animation. This won't be visible since we've added the snapshot on the rootVC
        [rootVC dismissViewControllerAnimated:NO completion:^{

            // present the new view controller that we need
            [rootVC presentViewController:viewController animated:YES completion:^{
                // pop to root in case there are more than one pushed to nav stack
                [rootVC popToRootViewControllerAnimated:NO];
                // we don't need the snapshot view anymore
                [snapShotView removeFromSuperview];
            }];

        }];
    } else {
        [rootVC presentViewController:viewController animated:YES completion:^{
            // we presented our vc from the nav vc, so we can pop the nav vs itself to root - that won't be noticable
            [rootVC popToRootViewControllerAnimated:NO];
        }];
    }

}

即使插入当前 View 层次结构的快照,我仍然有闪光。使用此解决方案 dismissViewControllerAnimated 会导致链中前一个 View Controller 的短暂闪光(一个展示 self 的人)。

有没有一种方法可以在不闪烁的情况下实现预期的结果?我想类似的事情可以通过自定义父 View Controller 和 subview Controller 来实现,从那时起就可以更好地控制 View 的方式被替换了,但我真的很想保持简单并使用 presentViewController:

最佳答案

您可以简单地在所有其他 Controller 之上呈现(动画)标签栏 Controller ,一旦动画完成,您可以将所有 Controller 解散回根并再次呈现标签栏 Controller ,这次不是动画.

确保您保留对标签栏 Controller 的引用,这样您就不必重新分配新版本。

关于ios - 呈现新的 View Controller 并无形地关闭当前呈现的 View Controller 链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33634705/

相关文章:

ios - Swift Obj-c 模型

ios - 从终端构建项目抛出配置文件错误

iphone - 有没有办法从字面上暂停 View ?

iphone - 在应用程序购买拒绝原因是什么意思?

ios - 在 UIWebview 中获取当前 URL 但它没有响应

ios - 从字符串 URL 数组下载图像并保存到文件?

iphone - Adobe Air/Actionscript/Flex 适用于 IOS iPhone 4 摄像头旋转 90 度

objective-c - iOS 内存管理 - 说明

iphone - 自定义 UITableViewCell 宽度在 tableview reloadData 上调整大小

iphone - iPhone 3GS iOS 4.0.1 下的 MPMoviePlayerController thumbnailImageAtTime EXC_BAD_ACCESS