2

Background: On iPad, I have a button which when tapped, showed a UIActionSheet. This action sheet has 2 options, camera and gallery. Camera when tapped, pulled up a camera and everything works fine. Gallery when tapped, suppose to show a popover with user's photos in it.

Problem: On iPad, UIActionSheet acts like a popover. Which when presenting, another popover cannot come into view. Error:Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.

My code:
Setting Action Sheet

- (void)imageButtonTapped:(UIButton *)sender
{
    if (_commentObject.image){
        _actionSheet = [[UIActionSheet alloc] initWithTitle:@"Action" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Remove" otherButtonTitles:nil];
        _actionSheet.tag = ACTION_IMAGE_REVIEW_TAG;
    }else{
        _actionSheet = [[UIActionSheet alloc] initWithTitle:@"Image Source" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Gallery", nil];
        _actionSheet.tag = ACTION_IMAGE_SOURCE_TAG;
    }

    if (_isPad) {
        [_actionSheet showFromRect:_imageButton.frame inView:_scrollViewContent animated:YES];
    }else{
        [_actionSheet showInView:self.view];
    }
}

Delegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (actionSheet.tag) {
        case ACTION_IMAGE_SOURCE_TAG:
            switch (buttonIndex) {
                case 0:
                    [self pickImage:YES];
                    break;
                case 1:
                    [self pickImage:NO];
                    break;
                default:
                    break;
            }
                break; 
}

Executing

- (void)pickImage:(BOOL)fromCamera
{
    if (fromCamera) {
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            UIImagePickerController* cameraPickerController = [[UIImagePickerController alloc] init];
            cameraPickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            cameraPickerController.delegate = self;
            [self presentViewController:cameraPickerController animated:YES completion:nil];
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Camera Unavailable" message:@"Your Device does not support Cameras" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
    }else{
        UIImagePickerController *galleryPickerController = [[UIImagePickerController alloc] init];
        galleryPickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        galleryPickerController.delegate = self;

        if (_isPad) {
            if ([_actionSheet isVisible]) {
                [_actionSheet removeFromSuperview];
                UIPopoverController *imagePickerPopover = [[UIPopoverController alloc] initWithContentViewController:galleryPickerController];
                [imagePickerPopover presentPopoverFromRect:_imageButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
            }
        }else{
            [self presentViewController:galleryPickerController animated:YES completion:nil];
        }
    }
}

Question: I tried removing the action sheet from view, and tried dismissing it before executing pickImage. None of that works. How do I present the gallery?

Byte
  • 2,920
  • 3
  • 33
  • 55
  • 1
    It's happening because you don't have a reference to your `UIPopoverController`. Have a strong reference for your imagePickerPopover and then try. No problem will occur. – Dinesh Raja Aug 12 '13 at 14:15
  • Oh right, I misdiagnosed it. Thank you. please post it as answer so I can accept it – Byte Aug 12 '13 at 14:29

3 Answers3

2

Your Problem: Error:Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.

make Object of UIPopoverController in your .h file class. Make sure that your @property for your UIPopoverController is strong instead of weak.

Check this

UIPopoverController: dealloc reached while popover is still visible

UIPopovercontroller dealloc reached while popover is still visible

Community
  • 1
  • 1
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • +1 for trying and almost getting it right. If you 'actually look' at my code, you will see the popover is defined within a scope and get released within the scope before it gets to dismiss. Nothing to do with .h file. The comment on the main question hit the nail in the head so I will ask him to post as an answer. – Byte Aug 12 '13 at 14:33
0

FYI, This was a comment and OP asked to put it as an answer. Because this solved the issue.

It's happening because you don't have a reference to your UIPopoverController. Have a strong reference for your imagePickerPopover and then try. No problem will occur.

Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
  • To elaborate on the answer, `UIPopoverController * imagePickerPopover` instant was created within a bracket and displayed. When it left the bracket, it was getting dealloc while still showing. To solve this, I replace the local variable `imagePickerPopover` with an instance variable which will persist throughout the life time of the instance. – Byte Aug 13 '13 at 14:40
0

you can try this:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        if ([actionSheet isVisible]) {
            [actionSheet dismissWithClickedButtonIndex:0 animated:NO];
        }
    }
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        UIImagePickerController *galleryPickerController = ......
    }
}
phemt.latd
  • 1,775
  • 21
  • 33