Eedoh
Eedoh

Reputation: 6278

Dismiss two modal (table)view controllers

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.

Upvotes: 4

Views: 555

Answers (3)

Roger C S Wernersson
Roger C S Wernersson

Reputation: 6562

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

Upvotes: 0

john.k.doe
john.k.doe

Reputation: 7563

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

Upvotes: 1

Maulik
Maulik

Reputation: 19418

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]

Upvotes: 2

Related Questions