我是iOS开发的新手,遇到了一个我似乎无法找到解决方案的错误。我到处寻找解决方案,但也许是我的新意让我无法看到问题。
日志中打印的确切警告是:
尝试从视图控制器中解除< _UIAlertShimPresentingViewController:0x7aaa4b90>正在进行演示或解雇!
在触摸actionSheet上的按钮后立即发生。
以下是代码:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
if (buttonIndex == 0) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
else if (buttonIndex == 1) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
else if (buttonIndex == 2) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:picker animated:YES completion:nil];
}
} else {
if (buttonIndex == 0) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:NO completion:NULL];
}
else if (buttonIndex == 1) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:picker animated:YES completion:NULL];
}
}
}
actionSheet的实现,我在IBAction上连接了一个位于.xib文件的工具栏按钮。
- (IBAction)addImage:(id)sender {
UIActionSheet *popUpSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles: nil];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[popUpSheet addButtonWithTitle:@"Camera"];
[popUpSheet addButtonWithTitle:@"Photo Library"];
[popUpSheet addButtonWithTitle:@"Camera Roll"];
[popUpSheet addButtonWithTitle:@"Cancel"];
popUpSheet.cancelButtonIndex = popUpSheet.numberOfButtons-1;
} else {
[popUpSheet addButtonWithTitle:@"Photo Library"];
[popUpSheet addButtonWithTitle:@"Camera Roll"];
[popUpSheet addButtonWithTitle:@"Cancel"];
popUpSheet.cancelButtonIndex = popUpSheet.numberOfButtons-1;
}
[popUpSheet showFromBarButtonItem: self.toolbarItems[0] animated:YES]; }
从我所知道的,所有事情都被正确授权:
DetailViewController.m
@interface DetailViewController () < UINavigationControllerDelegate, UIImagePickerControllerDelegate >
DetailViewController.h
@interface DetailViewController : UIViewController <UIActionSheetDelegate>
任何见解都会受到高度赞赏并非常有帮助。
答案 0 :(得分:1)
您的代码看起来是正确的。尝试使用:
actionSheet:didDismissWithButtonIndex:
方法。动画结束后,它会发送给代表。希望它有所帮助。
答案 1 :(得分:0)
终于找到了问题所在。编译器给了我这个警告,因为Apple最近将UIActionsheet和UIAlert组合成一个名为UIAlertController的控制器类型。使用了新的UIAlertController,我的问题解决了。
- (IBAction)addImage:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
UIAlertController *popUpSheet = [UIAlertController alertControllerWithTitle:nil message:@"Select your Choice" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *photoLibrary = [UIAlertAction actionWithTitle:@"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
NSLog(@"Photo Library was touched");
[popUpSheet dismissViewControllerAnimated:YES completion: nil];
}];
UIAlertAction *recentPhotos = [UIAlertAction actionWithTitle:@"Camera Roll" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:picker animated:YES completion:nil];
NSLog(@"Camera Roll was touched");
[popUpSheet dismissViewControllerAnimated:YES completion: nil];
}];
UIAlertAction *camera = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
NSLog(@"Camera was touched");
[popUpSheet dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[popUpSheet dismissViewControllerAnimated:YES completion:nil];
}];
[popUpSheet addAction:photoLibrary];
[popUpSheet addAction:recentPhotos];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[popUpSheet addAction:camera];
}
[popUpSheet addAction:cancel];
[self presentViewController:popUpSheet animated:YES completion:nil];
}