Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismissing 2 ViewController consecutively

I try 2 ways to dismissed 2 viewcontrollers consecutively but only one of them got dismissed not the second one

method1

-(void) LoginDone:(NSNotification *)notif 
 {
 [self dismissViewControllerAnimated:YES completion:NULL];   //previous viewcontroller
 [self dismissViewControllerAnimated:YES completion:NULL];   //current viewcontroller
  }

method2

-(void) LoginDone:(NSNotification *)notif 
{
[self dismissViewControllerAnimated:YES completion:NULL];
 [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginDone2" object:nil];

 }

-(void) LoginDone2:(NSNotification *)notif 
{
     [self dismissViewControllerAnimated:YES completion:NULL];


 }

I need to find out a way to dismiss both the previous viewcontroller and current viewcontroller consecutively.

like image 800
jason white Avatar asked Dec 06 '22 13:12

jason white


2 Answers

This is now an old question, but it seems to be exactly the problem I am having presently. Here what I did:

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

And it works for me. I hope it can be useful to someone.

like image 133
Michel Avatar answered Jan 04 '23 19:01

Michel


By calling

 [self dismissViewControllerAnimated:YES completion:NULL];

you are telling self to dismiss the view it presented. Telling twice the same self object to dismiss the view it presented, will not change the result. In other words self cannot represent the "current view" and the "previous view" at the same time as per your comment to the code. self is just a single controller representing a single view, either the current or the previous one.

To fix this, you should send the dismissViewControllerAnimated to self (that presented the top-most view, I assume) and to the other view controller object that presented the previous view.

In other words, I would expect something like this:

-(void) LoginDone:(NSNotification *)notif 
{
   [self dismissViewControllerAnimated:YES completion:NULL];
   [self.previousController dismissViewControllerAnimated:YES completion:NULL];
}

Actually, you could send just one message to the second view controller and both views would be dismissed (source):

If you present several view controllers in succession, and thus build a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

like image 33
sergio Avatar answered Jan 04 '23 19:01

sergio