简体   繁体   English

有没有办法在呈现另一个ViewController之后将其关闭?

[英]Is there a way to dismiss the current ViewController after presenting another?

After some time searching for an answer, I have to admit that i'm pretty confuse with this case. 经过一段时间的寻找答案后,我不得不承认我对这种情况非常困惑。

At my job, I'm asked to do something really specific : 在工作中,我被要求做一些非常具体的事情:

I have to present a UIViewController on a previous UIViewController, actually, the current ViewController dismiss itself before the second appear. 我必须在以前的UIViewController上显示一个UIViewController,实际上,当前的ViewController在第二个出现之前就关闭了。 That give the whole thing a funny animation that a ViewController goes down and another rise from the bottom after this. 这给整个事情带来了一个有趣的动画,一个ViewController下降,然后又从底部上升。 But... It's not really "pro" and, we're able to see the rootViewController behind the scene during the animation. 但是...并不是真正的“专业版”,在动画制作过程中,我们可以在幕后看到rootViewController。

So, I have to precise that there is NO NavigationController, that would have made this a lot easier in my opinion, so I'm forced to do it with two UIViewController, and there is my actual code : 因此,我必须精确地确定,没有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];
}];     

It is call right after a button is pressed, so there is no problem with the actual ViewController viewDidLoad or viewDidAppear I think. 它是在按下按钮后立即调用的,因此我认为实际的ViewController viewDidLoad或viewDidAppear没有问题。

But each time this code runs, I get the following error : 但是每次运行此代码时,都会出现以下错误:

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

I don't really know how I could manage to keep a trace of the current UIViewController to dismiss it in the next ViewController viewDidAppear to be sure there will be no "blackout" on the screen. 我真的不知道如何设法跟踪当前的UIViewController以便在下一个ViewController viewDidAppear中关闭它,以确保屏幕上不会出现“停电”。

Thank you for your help in advance! 提前谢谢你的帮助!

Try this code: 试试这个代码:

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];
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在关闭当前ViewController之后快速显示,然后Presenting ViewController不调用viewWillAppear() - Swift after dismiss Current ViewController then the Presenting ViewController not calling viewWillAppear() Swift 在呈现新的 ViewController 后关闭当前的 ViewController - Swift dismiss current Viewcontroller after presenting new ViewController 在使用Swift展示新的Viewcontroller后如何关闭以前的Viewcontroller? - How to dismiss previous Viewcontroller after presenting new Viewcontroller using Swift? 呈现另一个后关闭UIViewController - Dismiss UIViewController after presenting another one 在呈现新的视图控制器后关闭当前的视图控制器 - swift - dismiss current view controller AFTER presenting new view controller - swift 如何解除当前的ViewController并转到Swift中的另一个View - How to dismiss the current ViewController and go to another View in Swift 呈现后模糊Viewcontroller变暗 - Blur viewcontroller darkens after presenting 如何为另一个 ViewController 关闭 PopViewController - How to dismiss PopViewController for another ViewController 如何在另一个ViewController中关闭一个ViewController? - How to dismiss a viewcontroller within another ViewController? 在展示另一个控制器之前将其关闭 - dismiss view controller before presenting another one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM