Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close one modal ViewController and open another in same method. Possible?

I have a situation where I want to open one modal controller from the other. But when the user closes either, they should go back to the parent controller of both modal controllers.

So I have the parent controller in charge of this. The method on the parent does something similar to the code below when the user clicks a button on the first modal controller.

[self.navigationController dismissModalViewController:YES];
SecondModalViewController *c = [[SecondModalViewController alloc] init];
[self.navigationController presentModalViewController:c animated:YES];
[c release];

The only thing that happens is the first view closes, but I don't see the second view open. Is it possible to close a modal ViewController and open a second one in the same method? If so, how?

like image 350
ageektrapped Avatar asked Mar 29 '11 15:03

ageektrapped


2 Answers

i think that by the time you close the first view controller, the [self.navigation presentmodal..] dosen't have a self to open the new one from.

you could do that:

use a boolean variable to state whether the next time this controller appears (when you close the one you're currently trying to open), it should close, and implements that feature in the viewDidApper: method, like so:

@interface FirstViewController : UIViewController 
{
    //...
    BOOL close;
}

and in the .m file,

-(void)viewDidAppear:(BOOL)animated{
    if(close)
        [self dismissModalViewControllerAnimated:NO];
    else
        [super viewDidAppear:animated];
}

now, to open your new controller, do that:

-(IBAction)openSecondController:(id)sender{
    //..
    SecondViewController* controller = [[SecondViewController alloc]     initWithNibName:@"SecondViewController" bundle:nil];
    close = YES;
    [self presentModalViewController:controller animated:NO];
}

now, when you close the second one, the first one will close also. doesn't have any visible side affects in my apps..

not the cleanest way, but i didn't find a better one. cheers

like image 124
eiran Avatar answered Nov 05 '22 23:11

eiran


I do that same thing all the time without a problem, though mine have animated:NO... I'm guessing your issue is because you have animated:YES on both. What effect are you looking for exactly? Do you want to see one get animated away, and then the other get animated in? If so, you need to execute the presentModalViewController with a delay.

Otherwise, you should just be able to present the second modal view controller without closing the first one at all. When you call dismissModalViewController; it should dismiss both.

like image 32
GendoIkari Avatar answered Nov 06 '22 00:11

GendoIkari