Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to dismiss the presentation controller while transitioning already

Tags:

xcode

ios

I compile my project using Xcode6 GM on iOS8 GM. When dismissing many view controllers, my app always crashes and the debug area shows:

"Trying to dismiss the presentation controller while transitioning already. transitionViewForCurrentTransition is not set, presentation controller was dismissed during the presentation? "

I have googled and find a similar case and shows the same error:

[self.viewController presentViewController:vc animated:NO completion:^{         [self.viewController dismissViewControllerAnimated:NO completion:nil]; }]; 

It works fine using Xcode5 and iOS7 . What does the error means? Is iOS8 isn't happy with the "Hack"? Thanks in advance.

like image 653
ylovesy Avatar asked Sep 10 '14 09:09

ylovesy


People also ask

What is presentation controller?

The presentation controller's role during all of these phases is to manage its own custom views and state information. During the presentation and dismissal phases, the presentation controller adds its custom views (if any) to the view hierarchy and creates any appropriate transition animations for those views.


2 Answers

Are you trying to force a device orientation change? Anyway, in my opinion you could try to change your current code to:

[self.navigationController presentViewController:vc animated:NO completion:^{     dispatch_after(0, dispatch_get_main_queue(), ^{         [self.navigationController dismissViewControllerAnimated:NO completion:nil];     }); }]; 
like image 169
olivier Avatar answered Sep 20 '22 21:09

olivier


I had the same issue and I found a clean solution avoid using dispatch_async or dispatch_after.

Simply, as described by the exception, you are trying to dismiss a view controller while the presenting transition is still in progress. This means that once the

- presentViewController:animated:completion:  

completion block is called, and you invoke the dismiss, the transitioning is not completed.

Starting from iOS 7 transitioning UIViewController has a new method available

- transitionCoordinator  

The transitionCoordinator gives you the chance to enqueue a completion block as soon as the transition completes.

The object returned by the method conforms the UIViewControllerTransitionCoordinator protocol. Knowing that the solution is really simple.

After the invocation of

- presentViewController:animated:completion:  

the transition coordinator is properly configured by the framework.

Use

- animateAlongsideTransition:completion:  

on it to send the proper completion block.

Here a little code snippet that better explain the solution

void(^completion)() = ^() {     [modalViewController dismissViewControllerAnimated:YES completion:nil]; };  // This check is needed if you need to support iOS version older than 7.0 BOOL canUseTransitionCoordinator = [viewController respondsToSelector:@selector(transitionCoordinator)];  if (animated && canUseTransitionCoordinator) {     [viewController presentViewController:modalViewController animated:animated completion:nil];     [viewController.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {         completion();     }]; } else {     [viewController presentViewController:modalViewController animated:animated completion:completion]; } 
like image 30
Gabriele Avatar answered Sep 17 '22 21:09

Gabriele