iOS - 是否可以从模态呈现的视图控制器中呈现模态视图控制器?

时间:2015-11-03 04:15:10

标签: ios uiimagepickercontroller modalviewcontroller presentviewcontroller

我正在开发一个提供"编辑个人资料"屏幕。然后从那个屏幕我希望能够呈现另一个模态视图控制器,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"];
        }
    }
}

任何帮助非常感谢。感谢。

1 个答案:

答案 0 :(得分:4)

更改

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] initWithRootViewController:self];

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];