【问题标题】:Is there a way to dismiss the current ViewController after presenting another?有没有办法在呈现另一个 ViewController 后关闭当前的 ViewController?
【发布时间】:2015-02-09 18:37:25
【问题描述】:

在寻找答案一段时间后,我不得不承认我对这个案例很困惑。

在我的工作中,我被要求做一些非常具体的事情:

我必须在前一个 UIViewController 上呈现一个 UIViewController,实际上,当前的 ViewController 在第二个出现之前会自行关闭。这给整个事情一个有趣的动画,一个 ViewController 下降并在此之后从底部再次上升。但是……这不是真正的“专业”,而且我们能够在动画期间看到幕后的 rootViewController。

所以,我必须准确地说没有 NavigationController,在我看来这会使这变得容易得多,所以我不得不用两个 UIViewController 来做这件事,并且有我的实际代码:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
        UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"userViewController"];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:controller animated:YES completion:^{
            [self dismissViewControllerAnimated:YES completion:nil];
}];     

它是在按下按钮后立即调用的,所以我认为实际的 ViewController viewDidLoad 或 viewDidAppear 没有问题。

但是每次运行这段代码时,我都会收到以下错误:

[1163:17641] Warning: Attempt to present <UserViewController: 0x7b0f0a00> on <EIHomeViewController: 0x7b0d2a00> whose view is not in the window hierarchy!

我真的不知道如何设法跟踪当前 UIViewController 以在下一个 ViewController viewDidAppear 中将其关闭,以确保屏幕上不会出现“停电”。

提前感谢您的帮助!

【问题讨论】:

    标签: ios cocoa-touch ios7 uiviewcontroller


    【解决方案1】:

    试试这个代码:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
    UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"userViewController"];
    [self dismissViewControllerAnimated:NO completion:^{
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:controller animated:NO completion:nil];
    }];
    

    【讨论】:

    • 我之前的代码与解除动画的异常相同,两个视图控制器之间总是存在这种“停电”,而且我不再有“上升”动画。但是,是的,它在很大程度上减少了延迟。但是我正在寻找允许我在另一个上添加 ViewController 以在新的 ViewController viewDidAppear 之后关闭另一个 View 的东西。 :s
    • 但是如果你从当前的 vc 中呈现下一个 vc,你将有一个类似的堆栈:root->current->next。如果现在你在 current (next 的presentingViewController) 上调用dismissViewControllerAnimated,next 也会消失。
    • 没有选项可以通过root调用下一个?就像从堆栈中提取的 hack 或使用另一个使其出现在屏幕顶部的层?
    猜你喜欢
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多