在iPad上显示ImagePickerController来自ActionSheet

时间:2013-08-12 13:58:24

标签: ios ipad uiimagepickercontroller uiactionsheet uipopover

背景:在iPad上,我有一个按钮,当点按时,会显示UIActionSheet。此操作表有2个选项,相机和图库。拍摄时的相机,拉起相机,一切正常。点击图库时,假设显示包含用户照片的弹出窗口。

问题:在iPad上,UIActionSheet就像一个弹出窗口。在展示时,另一个弹出窗口无法进入视野。错误:Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.

我的代码:
设置行动表

- (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];
    }
}

代表

- (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; 
}

执行

- (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];
        }
    }
}

问题:我尝试从视图中删除操作表,并在执行pickImage之前尝试将其解除。这些都不起作用。我如何展示画廊?

3 个答案:

答案 0 :(得分:2)

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

在.h文件类中创建UIPopoverController的Object。确保UIPopoverController的@property强大而不是弱。

检查

UIPopoverController: dealloc reached while popover is still visible

UIPopovercontroller dealloc reached while popover is still visible

答案 1 :(得分:0)

仅供参考,这是一个评论,OP要求将其作为答案。因为这解决了这个问题。

这种情况正在发生,因为您没有对UIPopoverController的引用。为imagePickerPopover提供强有力的参考,然后尝试。没问题会发生。

答案 2 :(得分:0)

你可以试试这个:

-(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 = ......
    }
}