5

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.

Tar_Tw45
  • 3,122
  • 6
  • 35
  • 58
  • 1
    Have a look at here [Click](http://stackoverflow.com/questions/7429014/application-tried-to-present-modally-an-active-controller).Maybe helpful. – junkor Mar 06 '13 at 08:32
  • show us where you dismiss the VC – Daij-Djan Mar 10 '13 at 14:25
  • Thanks @junkor, but, I read that one already. The fact is, I think I read all of the questions related to this on SOF before I posted. Now, I decided to clear my head and come back to this later, redo the whole process and will see what what will happens. – Tar_Tw45 Mar 12 '13 at 04:20
  • @Daij-Djan It's not error when dismissing, it was when presenting. I added a break point at the dismiss code, which was never be reached. – Tar_Tw45 Mar 12 '13 at 04:21
  • you really don't want to show any code ^^ – Daij-Djan Mar 12 '13 at 09:43
  • @Daij-Djan Sorry mate, I was busy. I didn't show the dismiss code since it crash while trying to present. Like I said, I add a break point at the dismiss code, which never be reached. – Tar_Tw45 Mar 21 '13 at 03:04

5 Answers5

9

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. :)

JordiVilaplana
  • 485
  • 3
  • 9
4

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.

Arun
  • 476
  • 4
  • 6
  • I know that presentPopover is find for Camera Roll (Because I'm doing so), but is it fine too for Device Camera? – Tar_Tw45 Mar 12 '13 at 04:24
  • 1
    Hi, Check the apple documentation for more details. http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html – Arun Mar 13 '13 at 11:06
2

Have you tried to present it like this?

[self.view.window.rootViewController presentModalViewController:cameraPicker animated:YES];
CainaSouza
  • 1,417
  • 2
  • 16
  • 31
  • Yes, I did as I mentioned above. Present from RootViewController, TopViewController, PopoverViewController and TabBarController are result as the same. Appreciate your suggestion. – Tar_Tw45 Mar 04 '13 at 02:16
1

My guess is that the cameraPicker instance is not correctly allocated/released. Try creating the cameraPicker inside your - (void)takePhotoWithCamera method rather than relying on a previously created instance. You'll get a handle to the picker instance in the callback methods...

Johnny O
  • 587
  • 4
  • 4
0

I had the same problem - I wanted users to take photos using a full screen view (i.e. call presentViewController and pass UIImagePickerController controller instance) and select existing photos from a popover (I associated it with a popover using initWithContentViewController). I reused the same instance of UIImagePickerController for both camera and popover and it threw the same exception if I tried to run a camera before opening a popover.

I turned out to cause a problem and my solution was simply to have two instances of UIImagePickerController - one for camera (which I presented from a main view) and another one for popover. It works so far. :-)

Not sure if it is still actual for the original poster, but hopefully it will help anyone else who encounter this discussion.

Andrew Simontsev
  • 1,078
  • 9
  • 18