【发布时间】:2016-02-03 03:26:37
【问题描述】:
我正在开发一个显示“编辑个人资料”屏幕的应用。然后从那个屏幕我希望能够呈现另一个模态视图控制器 UIImagePickerController。但是,我无法这样做并继续收到以下错误:
*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“应用程序试图呈现 模态视图控制器本身。呈现控制器是 UIImagePickerController: 0x13d077000.'
在我的应用程序中,在我的个人资料屏幕上,我的右上角栏按钮项以模态方式呈现“编辑个人资料”屏幕,如下所示:
// MyProfileViewController.m
// ...
- (void)rightBarButtonItemTapped:(id)sender
{
EditMyProfileViewController *editMyProfileVC = [[EditMyProfileViewController alloc] init];
editMyProfileVC.delegate = self;
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:editMyProfileVC];
navVC.navigationBar.translucent = NO;
[self presentViewController:navVC animated:YES completion:^{}];
}
然后,在模态呈现的“编辑配置文件”屏幕的上下文中,我希望能够以模态呈现 UIImagePickerController。我尝试这样做:
// EditMyProfileViewController.m
// ...
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([actionSheet isEqual:self.profilePicActionSheet])
{
if (buttonIndex == 0) // Camera
{
[self.profilePicActionSheet dismissWithClickedButtonIndex:0 animated:YES];
[self showPhotoPickerUsingSourceType:UIImagePickerControllerSourceTypeCamera];
}
if (buttonIndex == 1) // Photo Library
{
[self.profilePicActionSheet dismissWithClickedButtonIndex:0 animated:YES];
[self showPhotoPickerUsingSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
if (buttonIndex == 2) // Saved Photos Album
{
[self.profilePicActionSheet dismissWithClickedButtonIndex:0 animated:YES];
[self showPhotoPickerUsingSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
}
}
}
- (void)showPhotoPickerUsingSourceType:(UIImagePickerControllerSourceType)sourceType
{
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
if (status != ALAuthorizationStatusAuthorized)
{
// ...
}
else // status == ALAuthorizationStatusAuthorized
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] initWithRootViewController:self];
imagePicker.sourceType = sourceType;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
[self presentViewController:imagePicker animated:YES completion:^{
NSLog(@"imagePicker is on screen..."); // <-- App crashes before we get here
}];
}
else
{
[Helper helperShowAlertWithTitle:@"Selected Source Type Not Available"];
}
}
}
非常感谢任何帮助。谢谢。
【问题讨论】:
-
什么是
self当您尝试展示图像选择器时? -
self是模态呈现的视图控制器,EditMyProfileViewController。EditMyProfileViewController位于UINavigationController中,这就是rightBarButtonItemTapped中实际呈现的内容。 -
@matt 我添加了更多代码细节。希望这会有所帮助。
标签: ios uiimagepickercontroller modalviewcontroller presentviewcontroller