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];
}
- (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];
}
}
//所以采用新的代理委托方法:也就是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];
}
}