【问题标题】:iOS - Is it possible to present a modal view controller from a modally presented view controller?iOS - 是否可以从模态呈现的视图控制器呈现模态视图控制器?
【发布时间】: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 是模态呈现的视图控制器,EditMyProfileViewControllerEditMyProfileViewController 位于 UINavigationController 中,这就是 rightBarButtonItemTapped 中实际呈现的内容。
  • @matt 我添加了更多代码细节。希望这会有所帮助。

标签: ios uiimagepickercontroller modalviewcontroller presentviewcontroller


【解决方案1】:

改变

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

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

【讨论】:

  • 好收获。我没有足够的滚动来注意到使用了错误的初始化程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多