Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I dismiss two modal view controller in succession *with animation*?

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?

like image 306
moon6pence Avatar asked Jul 19 '11 03:07

moon6pence


People also ask

How do you dismiss a modal view controller?

The first option is to tell the view to dismiss itself using its presentation mode environment key. Any view can read its presentation mode using @Environment(\. presentationMode) , and calling wrappedValue. dismiss() on that will cause the view to be dismissed.


2 Answers

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.

like image 88
Bill Patterson Avatar answered Nov 15 '22 20:11

Bill Patterson


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.

like image 28
Rahul Vyas Avatar answered Nov 15 '22 20:11

Rahul Vyas