jason white
jason white

Reputation: 711

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.

Upvotes: 4

Views: 4799

Answers (6)

matthiaswitt
matthiaswitt

Reputation: 196

You can use self.view.hidden=true; on the jumped viewcontrollers while animating back to the first viewcontroller. Using [self.presentingViewController dismiss...] is not working for me without hiding.

Upvotes: 0

Michel
Michel

Reputation: 11755

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.

Upvotes: 8

etayluz
etayluz

Reputation: 16436

I like JPetric's idea, but first you must dismiss the current view controller's view and only then can you dismiss the presenting view controller's view.

    [self dismissViewControllerAnimated:NO completion:^(void) {
        [self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
    }];

Upvotes: 1

JPetric
JPetric

Reputation: 3918

I know this is an old question but maybe somebody will look for solution on this issue so here it is:

-(void) closeModalViews 
{
    [previousVC dismissViewControllerAnimated:YES completion:^(void) {
        [self dismissViewControllerAnimated:YES];
    }];
}

Upvotes: 1

Munim
Munim

Reputation: 2778

As far as I could understand you are trying something like below:

  1. There are 2 view controllers.
  2. You want both of them to be vanished.
  3. Another controller comes in.

But the fact is only one controller is displayed, why you would need to dismiss 2 then?

Upvotes: 0

sergio
sergio

Reputation: 69047

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.

Upvotes: 5

Related Questions