iPad 弹出照片控制器失败


iPad上选择照片情况:

我们会用actionSheet弹出一个选择器,点击选择项推出照片控制器
但是ipad有一个问题是,在你将要推出照片选择器的时候,这个时候在controller上已经弹出了一个actionSheet,所以,系统会将你将要弹出的照片控制器进行取消操作。


解决办法:
在actionSheet的代理方法上,使用
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex这个方法进行操作
而不是- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex这个方法


这个情况是我遇见的:代码重现:

//弹出actionSheet选择器,并实现代理

- (void)chooseIconImage

{

    UIActionSheet *actionSheet;

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

    {

        actionSheet =[[UIActionSheet alloc]

                      initWithTitle:@"选择一张图片" delegate:self

                      cancelButtonTitle:@"取消" destructiveButtonTitle:nil

                      otherButtonTitles:@"拍摄新照片",@"从图库中选择",nil];

        

    }

    else {

        actionSheet =[[UIActionSheet alloc]

                      initWithTitle:@"选择一张图片" delegate:self

                      cancelButtonTitle:@"取消" destructiveButtonTitle:nil

                      otherButtonTitles:@"从图库中选择",nil];

    }

    

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];

    [actionSheet showInView:[UIApplication sharedApplication].keyWindow.rootViewController.view];

}


//actionSheetDelegate 代理方法
//在iOS8以前的方法里,直接在Click的委托事件里处理就好了,但是在iOS8之后,系统会抛出警告
Warning: Attempt to present <UIImagePickerController: 0x292b400>  on <**Controller: 0x723c150> which is already presenting <UIAlertController: 0xd37b8b0>
并且取消弹出ImagePicker行为。

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    UIImagePickerController *imagePickerController=nil;

    if(buttonIndex != actionSheet.cancelButtonIndex)

    {

        if(!imagePickerController)

        {

            imagePickerController = [[UIImagePickerController alloc] init];

            imagePickerController.delegate = self;

        }

        if(actionSheet.numberOfButtons==3)

        {

            imagePickerController.sourceType = (buttonIndex == 0) ?

            UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;

        }

        else

        {

            imagePickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

        }

        

        if(imagePickerController.sourceType == UIImagePickerControllerSourceTypeCamera){

            imagePickerController.allowsEditing = NO;

            

        }

        

        [self presentViewController:imagePickerController animated:NO completion:nil];

    }

}


警告的原因翻译过来就是因为已经有actionsheet存在了,不能present新的。此时我们选择新的委托方法

//所以采用新的代理委托方法:也就是didDismissWithButtonIndex

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{

    UIImagePickerController *imagePickerController=nil;

    if(buttonIndex != actionSheet.cancelButtonIndex)

    {

        if(!imagePickerController)

        {

            imagePickerController = [[UIImagePickerController alloc] init];

            imagePickerController.delegate = self;

        }

        if(actionSheet.numberOfButtons==3)

        {

            imagePickerController.sourceType = (buttonIndex == 0) ?

            UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;

        }

        else

        {

            imagePickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

        }

        

        if(imagePickerController.sourceType == UIImagePickerControllerSourceTypeCamera){

            imagePickerController.allowsEditing = NO;

            

        }

        

        [self presentViewController:imagePickerController animated:NO completion:nil];

    }


}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值