4

I know there's like 3-5 similar questions here, but non of the answers solves my problem.

I have a ViewController that opens a modal (table)view controller, which opens another one. Both modal view controllers are in fact table view controllers. I'm trying to dismiss both of them from the second one. I tried every accepted answer on similar question, none of them worked for me.

I tried

[self dismissModalViewControllerAnimated:true]

[self.parentViewController dismissModalViewControllerAnimated:true]
[self.parentViewController.parentViewController dismissModalViewControllerAnimated:true]

[self.presentingViewController dismissModalViewControllerAnimated:true]
[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:true]

When I try options 2, 3 and 5, nothing happens at all. When I use options 1, and 4, I see dismiss modal view animation and the underlying view itself for a moment, and then everything goes back to the second modal view (this time without animation).

I'm starting to think that this have something with the fact that I use tableViewControllers for modal views.

Btw, I'm dismissing modal views in didSelectRowAtIndexPath.

Eedoh
  • 5,818
  • 9
  • 38
  • 62
  • Did you try calling 4 then 1? – Omar Abdelhafith Jun 21 '12 at 11:21
  • I just tried your suggestion and I've got the program behavior just as I get when I use any of these options separately, on their own (I have a brief return to the underlying modal view, and then instant comeback to the last one) – Eedoh Jun 21 '12 at 11:24
  • http://stackoverflow.com/questions/4955638/dismiss-two-modal-view-controllers – pie Jun 21 '12 at 11:34
  • @pie. As you can see from my original post, I tried both those answers (from the post you are referencing to) and non of them solved my problem. – Eedoh Jun 21 '12 at 11:45

3 Answers3

2

Try this:-

When you dismiss your SecondView set a BOOL flag variable in app delegate file and check that variable in your FirstView's viewWillAppear method whether SecondView was open and close or not. If so, then [self dismissModalViewControllerAnimated:true]

Maulik
  • 19,348
  • 14
  • 82
  • 137
  • -1 Bad architecture option. You are creating a dependency between a `UIViewController` and the `AppDelegate`. When you develop you should think on the long term, and having that kind of approach goes against loose coupling. – Rui Peres Jun 21 '12 at 11:34
  • I was inspired by your solution, bug I don't use delegate files. I don't even know what they are, actually. – Roger C S Wernersson Jul 03 '15 at 08:56
1

typical model view controller behavior would suggest that you dismiss the modal view controller from the calling view controller rather than from self. not a hard and fast rule, but good practice.

to accomplish this, create a protocol:

    @protocol MyModalViewControllerDelegate
    - (void)modalViewControllerDidFinish;
    @end

and make both the parentViewController and FirstModalViewController be implemntors of this protocol.

    @interface FirstModalViewController <MyModalViewControllerDelegate>

then in both FirstModalViewController.h and SecondModalViewController.h, add:

    @property id<MyModalViewControllerDelegate> modalViewControllerDelegate

in both parentViewController and FirstModalViewController, right before calling presentModalViewController:... , set the following:

    modalViewControllerAboutToAppear.modalViewControllerDelegate = self;
    [self presentModalViewController:modalViewControllerAboutToAppear animated:YES];

next, in the SecondModalViewController, in the code where you determine that the item needs to be dismissed, call

    [self.modalViewControllerDelegate modalViewControllerDidFinish];

now, in FirstModalViewController, implement the following:

    - (void)modalViewControllerDidFinish:(MyModalViewController*)controller {
        [self dismissModalViewControllerAnimated:YES]
        [self.modalViewControllerDelegate modalViewControllerDidFinish];
    }

and finally, in the parent view controller, you should be able to perform:

    - (void)modalViewControllerDidFinish:(MyModalViewController*)controller {
        [self dismissModalViewControllerAnimated:YES]
    }
john.k.doe
  • 7,533
  • 2
  • 37
  • 64
0

Since I don't use delegate files, I did the following:

To FirstView add field

BOOL mClose;

To FirstView add method

- (void)close
{
    mClose = YES;
}

To FirstView method viewDidAppear add

if (mClose)
{
    [self dismissModalViewControllerAnimated:YES];
}

To FirstView method which opens SecondView add

[secondView closeWhenDone:self];

To SecondView add field

FirstView *mParent;

To SecondView add method

- (void)closeWhenDone:(FirstView*)parent
{
    mParent = parent;
}

To SecondView method which closes it add

[mParent close];
Roger C S Wernersson
  • 6,362
  • 6
  • 34
  • 45