Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application tried to present modally an active controller : UIImagePickerController

I'm struggle at this for 2 days and believe that this is the moment I should call for help. After I search SOF for a while, none of any answer could solve my problem. Here are my application ...

In the application,

  • Device is iPad, iOS 6
  • RootViewController is NavigationController
  • TopViewController is TabBarController
  • In this TabBarController, I present a popoverController from right bar button of navigation bar
  • In presenting popover there is a button to allow user to pick image from by taking new one or pick from existing.
  • To pick new one, I presentViewController UIImagePickerController to allow user to take photo with divice camera. presentModalViewController:animated: if iOS < 6, and presentViewController:animated:completion: for iOS > 6
  • I also hide Status Bar before presentation
  • To select from existing photo, I do presentPopoverFromBarButtonItem:permitArrowDirections:animated:
  • PopoverViewController also referencing by A TabBarController

Here is the issue

  • Present UIImagePickerController will always failed if user try to pick new one first with exception "Application tried to present modally an active controller <[name of view controller that try to present]>"
  • BUT, if user try to pick image from camera roll for once and then try to take new one again, it won't fail.

Here are what I tried

  • present from RootViewController
  • present from TopViewController (TabBarController)
  • present from popoverViewController itself
  • present from a tab of TabBarController
  • hide popoverViewController before presentation
  • resignFirstResponder from a textField in popoverViewController

Here is the current code I'm using

// PopoverViewController, presented by a tab in TabBarController
- (IBAction)takePhoto:(id)sender {
    [self.delegate takePhotoWithDeviceCamera];
}

// A Tab in TabBarController, delegate of popoverViewController
- (void)takePhotoWithCamera {
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    if ([UIDevice OSVersion] < 6.0) {
        [self presentModalViewController:cameraPicker animated:YES];
    } else {
        [self presentViewController:cameraPicker animated:YES completion:nil];
    }
}

Any idea what would cause this error? Any suggestion are welcome. Thank you.

like image 909
Tar_Tw45 Avatar asked Mar 01 '13 11:03

Tar_Tw45


3 Answers

Got the same trouble than you and finally got the solution based on @CainaSouza's answer. I've been working with Xamarin.iOS so I'll make my answer in C#, but it can be easily translated to Objective-C.

I'm using the same code as @CainaSouza to call the controller:

UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController (customController, true, null);

And then I add the following code to my custom RootViewController:

public override void PresentViewController (UIViewController viewControllerToPresent, bool animated, Action completionHandler)
{
    if (PresentedViewController != viewControllerToPresent) {
        base.PresentViewController (viewControllerToPresent, animated, completionHandler);
    }
}

The trick is to check if you haven't presented that UIViewController before.

I know it's an old question, but hope it will help someone. :)

like image 163
JordiVilaplana Avatar answered Oct 20 '22 09:10

JordiVilaplana


Present the imagePicker controller in a popoverController(in case of iPad). This will not give you that error.

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:self.selectedImageView.bounds inView:self.selectedImageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popOver = popover;
} 
else {
[self presentModalViewController:picker animated:YES];
}

Best Regards.

like image 4
Arun Avatar answered Oct 20 '22 10:10

Arun


Have you tried to present it like this?

[self.view.window.rootViewController presentModalViewController:cameraPicker animated:YES];
like image 2
CainaSouza Avatar answered Oct 20 '22 11:10

CainaSouza