daspianist
daspianist

Reputation: 5525

"Application tried to present modally an active controller" despite putting in various safe guards

I receive this exceptional only occasionally (about 20% of the times) when my controller attempts to present the modal UIImagePickerController. I have researched this topic quite a bit on SO, and have followed the instructions of similar questions on this topic about putting various safeguards. While this was able to reduce the frequency that I have gotten this error, I still receive it and am unsure what I could do to prevent it from happening.

My set up is the following: my root controller is a Tab Bar controller, in which I then have embedded a navigation controller. Inside this navigation controller I have TakePhoto1Controller, and I present my ImagePickerController modally when someone wants to take a picture.

My code is as the following:

BOOL modalPresent = (BOOL)(self.presentedViewController);
    //Present the Camera UIImagePicker if no image is taken
    if (!appDelegate.imageStorageDictionary[@"picture1"]){
        if (modalPresent == NO){ //checks if the UIImagePickerController is already modally active
            if (![[self imagePickerController] isBeingDismissed]) [self dismissViewControllerAnimated:NO completion:nil];
            [self presentViewController:self.imagePickerController animated:NO completion:nil];
        }
    }

The error I am getting is the following:

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller .'"

Upvotes: 2

Views: 3052

Answers (1)

Xu Yin
Xu Yin

Reputation: 3940

Your logic is a little bit strange to me, you are trying to dismiss imagePickerController and present the same view controller again. The error message is basically telling you imagePickerController is presented already.

Maybe you can change your logic to

if (![[self imagePickerController] isBeingDismissed])
{ [self dismissViewControllerAnimated:NO 
                          completion:^{
                               [self presentViewController:self.imagePickerController animated:NO completion:nil];
                           }];
}
else 
  [self presentViewController:self.imagePickerController animated:NO completion:nil];

Don't know if that's exactly what you looking for. But hope that gives you some hints.

Upvotes: 4

Related Questions