iOS - 是否可以从模态呈现的视图控制器呈现模态视图控制器?
iOS - Is it possible to present a modal view controller from a modally presented view controller?
我正在开发一个显示 "edit profile" 屏幕的应用程序。然后我希望能够从该屏幕显示另一个模态视图控制器 UIImagePickerController。但是,我无法这样做并继续收到以下错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present
modal view controller on itself. Presenting controller is
UIImagePickerController: 0x13d077000.'
在我的应用程序中,在我的个人资料屏幕上,右上角的栏按钮项以模态方式显示 "edit profile" 屏幕,如下所示:
// 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:^{}];
}
然后,在模态呈现的 "edit profile" 屏幕的上下文中,我希望能够模态呈现 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"];
}
}
}
非常感谢任何帮助。谢谢
改变
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] initWithRootViewController:self];
至
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
我正在开发一个显示 "edit profile" 屏幕的应用程序。然后我希望能够从该屏幕显示另一个模态视图控制器 UIImagePickerController。但是,我无法这样做并继续收到以下错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modal view controller on itself. Presenting controller is UIImagePickerController: 0x13d077000.'
在我的应用程序中,在我的个人资料屏幕上,右上角的栏按钮项以模态方式显示 "edit profile" 屏幕,如下所示:
// 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:^{}];
}
然后,在模态呈现的 "edit profile" 屏幕的上下文中,我希望能够模态呈现 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"];
}
}
}
非常感谢任何帮助。谢谢
改变
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] initWithRootViewController:self];
至
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];