views:

607

answers:

1

I would like to present multiple modal views in sequence (e.g. show confirmation page after selecting a picture from image picker). My problem is that the animation of dismissal and presenting in subsequent steps without delay always crashes the app with an EXC_BAD_ACCESS.

I assume that the problem is that CoreAnimation does not distinguish between the two transitions and cannot detect properly whether the first one has ended or not.

My work around so far is to introduce a 1 section delay which seems to solve the problem. However, I think that this makes the code a bit fragile. Is there another workaround?

Is this a bug in UIKit and should I submit a bug report?

Sample code

Here is a simple case that reproduce the crash:

  1. Create a new View-based project with the following class as the implementation of the main controller

  2. Hit 'cancel' when image picker view is presented

Expected behavior: the picker view is dismissed and presented again due to the subsequent call in viewDidAppear.

Actual behavior: It crashes with the stack trace presented below.

Code:


#import "SampleViewController.h"

@implementation SampleViewController

- (void)showModal {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    [self presentModalViewController:picker animated:YES];
    // [picker release];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self showModal]; // this line crashes the app
    // the following works as desired
    // [self performSelector:@selector(showModal) withObject:nil afterDelay:1];
}

@end
Crash Stack trace:
#0  0x30b43212 in -[UIWindowController transitionViewDidComplete:fromView:toView:]
#1  0x3095828e in -[UITransitionView notifyDidCompleteTransition:]
#2  0x3091af0d in -[UIViewAnimationState sendDelegateAnimationDidStop:finished:]
#3  0x3091ad7c in -[UIViewAnimationState animationDidStop:finished:]
#4  0x00b54331 in run_animation_callbacks
#5  0x00b54109 in CA::timer_callback
#6  0x302454a0 in CFRunLoopRunSpecific
#7  0x30244628 in CFRunLoopRunInMode
#8  0x32044c31 in GSEventRunModal
#9  0x32044cf6 in GSEventRun
#10 0x309021ee in UIApplicationMain
#11 0x00002794 in main at main.m:14
+4  A: 

Chances are you need to let the animation context finish. As you already discovered

[self performSelector:@selector(showModal) withObject:nil afterDelay:1];

works, but obviously, having a delay is no good, so do this:

[self performSelector:@selector(showModal) withObject:nil afterDelay:0.0];

When you use afterDelay:0.0 it doesn't directly call the selector, instead it enqueues the invocation on your runloop, which lets all of your state (autorelease pools, animation contexts, etc) uwnind correctly then immediately calls your invocation when the runloop starts processing events.

The one issue that might croup is that users could get UIEvents going off by tapping the screen, but you can fix that with by calling this before you are animation starts

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

and this once you have your final modal on the screen

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

Generally you want to spend UI interactions while you are animating quick transitions anyway.

Louis Gerbarg
using 0.0 as delay solves the problem! Thanks!
notnoop