Question

I have to add two modal dialogs right after the login window in my app before to go to the default tabViewContrller in my app. I have provided two modal dialogs in Storyborad and I did a quick testing to bring two modal dialogs one after another but none it's a perfect way to handle it.

I tried this in my first modal dialog when user tried to dismiss the current modal and has to go to second modal dialog:

-(IBAction)continueAction1:(id)sender{


  UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
  ModalViewController_alert *modalViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"second_modal_alert"];
  [modalViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
  [modalViewController setModalPresentationStyle:UIModalPresentationFormSheet];
  [self presentViewController:modalViewController animated:YES completion:nil];
CGRect frame = CGRectMake(self.view.superview.center.x - 200,self.view.superview.center.y + 100 -100,400,300);
modalViewController.view.superview.frame = frame;

 [self dismissViewControllerAnimated:YES completion:nil];//dismiss the first modal doesn't dispose it ??

}

and in the second modal dialog when user click on "Continue" button I did this in the class

-(IBAction)continueAction2:(id)sender{
  FFAppDelegate *appDelegate = (FFAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate doSomeExtensiveTaskInAThread_Cleanup];


   [self dismissViewControllerAnimated:YES completion:nil];//dismiss second one.

}

I also tried to use segue instead of first continueAction1 but don't know why after dismissing second dialog it shows me the first dialog for a moment before it goes to default tabViewController in the app(based on the extensive task in a thread as commented in my code).

Can someone show me a good approach to bring two modal dialog right after each other? what is wrong in my approach?

Was it helpful?

Solution

When you chain modal presentations together, you can dismiss them all at once by having the controller that did the original presentation call dismissViewControllerAnimated:completion:. So in, continueAction1, don't dismiss the first modal, just present the second one. In continueAction2, reference the first controller with self.presentingViewController.presentingViewController to do the dismissal:

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

You can also do this with modal segues, in which case you can use an unwind segue in the last controller to go back to wherever you want, no matter how many modal segues were chained together.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top