views:

116

answers:

3

I'm dismissing a modal view controller and then immediately presenting another one, but the latter never happens. Here's the code:

 [self dismissModalViewControllerAnimated:YES];

 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
 picker.delegate = self;
 picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
 [self presentModalViewController:picker animated:YES];

The first modal VC slides down, but the new picker never comes up. Any idea as to what's going on?

+2  A: 

What's happening is that the view controller removes it's reference to the modal view controller once the dismiss animation is done, which happens after this code is called, so it doesn't think it has a new view controller to present modally.

How I've dealt with this is to set a didDismissModalVC ivar to YES after I call dismissModalViewController. Then in my viewDidAppear: method, I check the value of the ivar and present then new modal view controller. (Remembering to also set the value back to NO so I don't get stuck eternally dismissing modal view controllers.)

Robot K
Another good way to deal with it, thanks Robot K.
James Skidmore
+3  A: 

Like other animated things, dismissModalViewControllerAnimated doesn't block until the view controller disappears. Instead it "kicks off" dismissal of the view controller. You might need to use a callback in viewDidDisappear of modal controller 1 that calls something like modalViewControllerDisappeared in the parent view controller. In that method you present modal controller 2. Otherwise what Robot K said.

Nimrod
That's a good idea. I might try it the next time I have to do this.
Robot K
Not sure it will work (I didn't test it) but it seems like it should.
Nimrod
Worked great, thanks Nimrod.
James Skidmore
+1  A: 

[self dismissModalViewControllerAnimated:NO];

UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; [self presentModalViewController:picker animated:YES];

Siddharth Iyer
set animation to NO for dismissing it.
Siddharth Iyer