Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception in iPhone app : Modal transition is already in progress

I have what I believe is a fairly simple application at the moment based on a few tutorials cobbled together. I'm using XCode 3.2.3 in OSX 10.6.4. It started as a standard iPhone "Window Based Application". Using interface builder I have added a Tab Bar Controller using the O'Reilly video tutorial here:

http://broadcast.oreilly.com/2009/06/tab-bars-and-navigation-bars-t.html

In the first Tab I have a standard UIView with two buttons. Both call the same function to display a UIImagePickerController:

-(IBAction) btnPhotoClicked:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
if((UIButton *)sender == btnChoosePhoto)
{
    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}

[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}

I am running the code inside an emulator so only ever click the button called Choose Photo. When the dialogue is released with a photo chosen this function runs:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary  *)info {
NSURL *mediaUrl;

mediaUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];

if (mediaUrl == nil)
{
    imagePuzzle = (UIImage *) [info valueForKey:UIImagePickerControllerEditedImage];
    if(imagePuzzle == nil)
    {
        //--- Original Image was selected ---
        imagePuzzle = (UIImage *) [info valueForKey:UIImagePickerControllerOriginalImage];
    }
    else {
        //--- Get the edited image ---
        //--- If it was successful the above valueForKey:UIImagePickerControllerEditedImage
        //--- would have assigned it already.
    }
}
else {
    //--- Muppet selected a video
}

// Animate the picker window going away
[picker dismissModalViewControllerAnimated:YES];
ImageViewController *imageViewController = [[ImageViewController alloc] init];
imageViewController.delegate = self;    
[self presentModalViewController:imageViewController animated:YES];
[imageViewController release];
}

This is where my problem lies. I've tried many different hacks and iterations but the above code is the simplest to present the problem. When the imageViewController is displayed as a modal dialogue the following exception is thrown:

2010-07-09 15:29:29.667 Golovomka[15183:207] *** Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'Attempting to begin a modal
transition from <NewViewController: 0x5915f80> to <ImageViewController: 0x594a350>     
while a transition is already in progress. Wait for viewDidAppear/viewDidDisappear
to know the current transition has completed'

How do I cure this? I have tried delays and other tricks but do not really understand how I'm supposed to use viewDidAppear or viewDidDisappear to help me. Also of note is that a very basic application with one view loading the picker then displaying another view with the image in does not produce the error. Any help gratefully received.

like image 537
Diziet Avatar asked Jul 09 '10 14:07

Diziet


2 Answers

To address the specific issue described here, you could add the viewDidAppear method in your class:

-(void)viewDidAppear:(BOOL)animated
{
    if (/*just visited ImagePicker*/)
    {
        ImageViewController *imageViewController = [[ImageViewController alloc] init];
        imageViewController.delegate = self;    
        [self presentModalViewController:imageViewController animated:YES];
        [imageViewController release];
    }
}

Remove those lines from below your call:

[picker dismissModalViewControllerAnimated:YES];

So, whenever your class self appears (is displayed), it will call viewDidAppear... Since this most likely isn't really what you want all the time, you could add some variables to set/clear that defines whether or not to immediately present the imageViewController when self is displayed. Something like "If coming from image picker, show the imageViewController, otherwise do nothing".

That said, imho, pushing modal views is should generally be done in response to a user action and I would maybe rethink the user experience here - e.g. add a subview instead of pushing a modal view which you could do where your currently have the code - but if you're just playing around with some tutorials that should solve the NSInternalInconsistencyException. :) Cheers!

like image 103
Eric Avatar answered Oct 05 '22 23:10

Eric


In iOS 5.0 and above you can use

[self dismissViewControllerAnimated:YES completion:^{
  //present another modal view controller here
}];
like image 37
Luong Huy Duc Avatar answered Oct 06 '22 00:10

Luong Huy Duc