关闭2个模态视图控制器 [英] Dismiss 2 modal view controllers

查看:105
本文介绍了关闭2个模态视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的模态视图控制器。然后我创建第二个:

i have a base modal view controller. i then create a 2nd one with:

FieldReportViewController *fieldReport = [self.storyboard instantiateViewControllerWithIdentifier:@"fieldReportView"];
[self presentModalViewController:fieldReport animated:YES];

从那里,我打电话给第三个视图控制器:

from there, i call a 3rd view controller with:

ScoutingReportViewController *scoutingReport = [self.storyboard instantiateViewControllerWithIdentifier:@"ScoutView"];
[self setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:scoutingReport animated:YES];

现在我想返回第一个视图控制器。我已经尝试过(这是目前显示的视图控制器):

now i want to return back to the first view controller. i've tried (this is currently shown view controller):

-(IBAction)doneBtn
{
    //[self.parentViewController dismissModalViewControllerAnimated:YES];  //removed

    [self dismissModalViewControllerAnimated:NO];    //added

    [[NSNotificationCenter defaultCenter] postNotificationName:@"CloseAllModalViews" object:nil userInfo:nil];  //added

}

在中/秒视图控制器中,我添加viewDidLoad中的观察者:

in middle/s view controller, i add an observer in viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(dismissModals:)
                                             name:@"CloseAllModalViews"
                                           object:nil];

和方法:

-(void) dismissModals:(id)sender
{
    [self dismissModalViewControllerAnimated:NO];
}

按下时,它什么都不做。它不会忽视任何观点。

when pressed, it does nothing. it does not dismiss any views.

我有什么想法可以让它发挥作用?

any ideas how i can get this to work?

编辑:计算为什么它崩溃了;我忘了我在IB中添加了一个segue到我的完成按钮。这会导致内存问题并导致崩溃。

figured out why it was crashing; i had forgotten i added a segue to my done button in IB. this lead to memory issues and was causing the crash.

编辑2:这会将两个视图控制器都关闭回基本/第一个视图控制器。但它并没有给我任何动画。我可以在没有动画的情况下解散当前的VC并用动画解雇中间的VC,但是在转换之前我会看到中间的VC。如果可能的话,我更愿意拥有从当前VC到基础VC的动画。

EDIT 2: this will dismiss both view controllers back to the base/first view controller. it does not give me any animations though. i can dismiss current VC with no animate and dismiss middle VC with animation, but i see the middle VC briefly before the transition. i'd prefer to to have an animation from the current VC to the base VC if possible.

推荐答案

我之前做过这个,我不得不使用UINotificationCenter来关闭中间视图。当您按下doneBtn时,发布通知。在推送其他视图之前,添加一个带有dismissModal作为操作的观察者。如果您删除动画,它应该看起来就像您回到原始视图。

I have done this before, and I had to use UINotificationCenter to dismiss the middle views. When you press your doneBtn, post a notification. Before you push your other views, add an observer with your dismissModal as the action. If you remove the animation, it should look like you just went back to the original view.

在您的视图中,您想要自动关闭:

In your view you want to auto dismiss:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(dismissModals:)
                                                 name:@"CloseAllModalViews"
                                               object:nil];

将它放在你的doneBtn方法中:

Put this in your doneBtn method:

[[NSNotificationCenter defaultCenter] postNotificationName:@"CloseAllModalViews" object:nil userInfo:nil];

这篇关于关闭2个模态视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆