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

I have a situation where I want to open one modal controller from another. But when the user closes, they must return to the parent controller of both modal controllers.

So, I have a parent controller responsible for this. The parent method does something similar to the code below when the user presses 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 closure of the first view, but I do not 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?

+6
iphone cocoa-touch modalviewcontroller
source share
4 answers

I do the same thing all the time without problems, although I have animated:NO ... I assume that your problem is that you have animated:YES for both. What effect are you looking for exactly? Do you want to see how someone is animated, and then another is animated? If so, you need to execute the current ModalViewController with a delay.

Otherwise, you should just be able to present the second modal view controller without closing the first. When you call rejectModalViewController; he must fire both.

+2
source share

I think that by the time you close the first view controller, [self.navigation presentmodal ..] does not have its own to open a new one.

You can do it:

use a boolean to indicate whether this next time this controller appears (when you close the one you are currently trying to open), it should close and implement this function in the viewDidApper: method, for example:

 @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 the new controller, do the following:

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

now that you close the second, the first will be closed. has no visible side effects in my applications.

not the cleanest way, but I did not find a better one. greetings

+7
source share

I find the exact same answer. This is similar to standard behavior.

The way I got around this is to use performSelector: withObject: afterDelay:

 [self performSelector:@selector(presentController:) withObject:navController afterDelay:0.5f]; 

The only drawback I had to set the function currentController, which then executed the current ModalViewController: animated:

I tried using NSInvocation, but when I tried to pass BOOL for the animated argument, I kept getting an invalid access error.

The main thing is that it works, and my client is happy.

0
source share

You can use the uiviewcontroller handle closed and open another using Unwind Segues.

https://developer.apple.com/library/content/technotes/tn2298/_index.html

0
source share

All Articles