11

Yes, I know there's exactly same question, but I want to do this with double dismiss animation. How to Dismiss 2 Modal View Controllers in Succession?

my code is same as question above,

view controller A (in navigation controller) - modal view controller B - modal view controller C

here's pseudo code

notification to B (as delegate)      // I changed order of this two lines, 
dismiss C *without* animation    // but it was same. 

(notification from C, in B)
dismiss B *with* animation

If I use animation when I dismiss C, it doesn't work, B will not be dismiss, because dismiss animation of C is playing.

Problem is: I can't start another dismiss animation if there's animation playing.

Can I play dismiss animation in succession?

This is not only problem of dismiss animation, it can be also applied to other iOS animations.

P.S: I think I can use timer to wait until first animation ends, but it is dirty and not stable way, isn't it?

Small Talk: In my program,

  • A: article list view
  • B: write article view
  • C: login view (if user is not logged in)

today, I have to add join view, I have to dismiss 3 views in time lol how can I help this?

Community
  • 1
  • 1
moon6pence
  • 712
  • 9
  • 24
  • More generic way to dismiss more that one modal view controllers is [here](https://stackoverflow.com/a/44583711/1151916) – Ramis Jun 16 '17 at 07:54

7 Answers7

24

iOS 5.0 and higher:

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

This works if you have:

A - starting view controller
M1 - modally presented by A
M2 - modally presented by M1

Put that line of code in M2's view controller class (and a button or whatever to activate it), and it will "reach up" through the hierarchy and tell A to dismissViewControllerAnimated:...

However,Rahul Vyas's answer is probably a better approach for cases where you have an unknown number of modal view controllers stretching between "A" and the last Modal view.

Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
Bill Patterson
  • 2,495
  • 1
  • 19
  • 20
  • 1
    Note that this doesn't dismiss with two animations. There's only one animation. After reading more carefully, I see that the OP specifically asked for two animations. My bad. – Bill Patterson Mar 07 '13 at 03:46
  • Here is the problem recurring in iOS 8.4 http://stackoverflow.com/questions/31396193/impossible-animation-with-dismissviewcontrolleranimated-ios8-4 – Chisx Jul 14 '15 at 02:04
7

You can generate a NSNotification and then from the root where your first modal appears dismiss the first modal view controller and all the others will automatically disappear. I have done this in one of my app.

Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
  • How do I get the first modal view controller in my root controller? Considering it could be one of many different classes – Gil Sand Jan 15 '15 at 11:04
  • You should definitely never use a NSNotification for this kind of communication! But otherwise.. this answer is 5 years old, maybe it was different in 2011. @Zil: you can while-loop through the presentingViewController property of your controller to get the root controller. – Ben Jun 22 '16 at 16:24
  • I ended up always stacking modals on a modal navigation controller and dismissing the navigation controller instead of the modals one by one – Gil Sand Jun 22 '16 at 16:25
0

[self dismissModalViewControllerAnimated:(BOOL)] doesn't work. The second animation doesn't fire. In iOS 5 you can use [self dismissViewControllerAnimated:YES completion:^{}]; but that's not backwards compatible to 4.0. I've only had success calling a delegate method that closes the modal view controller before presenting the new one.

earnshavian
  • 1,864
  • 2
  • 13
  • 17
0

Here is what worked form me, I had A->B, and then a container view "C "over B. Then I wanted C to dismiss back over to A,

The best way that worked for me was calling this my action in view C

self.presentingViewController?.dismiss(animated: true, completion: {})
self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: {})

This dismissed the Modal C, then dismisses B in a synchronous order. It worked better with the feel of my application

Ryan B.
  • 99
  • 2
  • 14
0

You can use [self dismissModalViewControllerAnimated:(BOOL)] when you want the view to dismiss the modal view. If you call this in both controllers it should work. I have not tried it myself but it seems logical.

I must add that if you need to present multiple modal views in a row maybe you should consider using different paradigms for some of them. For example, the login view could be an alertview instead of a modal view controller.

Pete42
  • 916
  • 7
  • 15
0

I've run into similar types of issues trying to get animations to work in succession. I've been meaning to try the following:

what if you put the call for the second animation (i.e. to dimiss B) inside a call to performSelectorOnMainThread? That would make me think the second animation would be forced to wait for the first to complete.

I haven't tested it yet though. Good luck - I'm very curious about what solution you come up with.

kris
  • 2,516
  • 25
  • 29
0

You can remove 2 view by using

[AviewController dismissModalViewControllerAnimated:YES];

Here AviewController is object of A. Hope this will help you.

Vijay
  • 997
  • 1
  • 12
  • 27