iPad 弹出照片控制器失败

来源:互联网 发布:断电后mysql无法启动 编辑:程序博客网 时间:2024/05/21 08:40


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([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

    {

        actionSheet =[[UIActionSheetalloc]

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

                      cancelButtonTitle:@"取消"destructiveButtonTitle:nil

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

        

    }

    else {

        actionSheet =[[UIActionSheetalloc]

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

                      cancelButtonTitle:@"取消"destructiveButtonTitle:nil

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

    }

    

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];

    [actionSheet showInView:[UIApplicationsharedApplication].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 = [[UIImagePickerControlleralloc] 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;

            

        }

        

        [selfpresentViewController:imagePickerControlleranimated:NOcompletion:nil];

    }

}


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

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

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

    UIImagePickerController *imagePickerController=nil;

    if(buttonIndex != actionSheet.cancelButtonIndex)

    {

        if(!imagePickerController)

        {

            imagePickerController = [[UIImagePickerControlleralloc] 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;

            

        }

        

        [selfpresentViewController:imagePickerControlleranimated:NOcompletion:nil];

    }


}







0 0
原创粉丝点击