2

在我的一个UINavigationController观点中,用户可以使用“添加”按钮从相册中选择照片UIImagePickerController。当用户选择所需的照片时,我想让他AddPhotoViewController查看他可以添加标题和描述的位置,而不是他可以按“保存”按钮保存数据、关闭AddPhotoViewController视图并返回上一个UINavigationController视图。到目前为止,我只能推送AddPhotoViewController视图,不能将其作为模态视图。我就是这样做的:

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
      AddPhotoViewController *apvc = [self.storyboard instantiateViewControllerWithIdentifier:@"addPhoto"];
      apvc.info = info;
      [self.navigationController pushViewController:apvc animated:YES];
      [self dismissViewControllerAnimated:YES completion:nil];
   }

上面的代码工作正常,但我想添加 AddPhotoViewController 作为 modal view 。我想使用presentModalViewController:animated:,但它在 iOS 6.0 中已贬值。比我试过的:

   [self presentViewController:apvc animated:YES completion:nil];

代替:

   [self.navigationController pushViewController:apvc animated:YES];

但我收到此错误:

   Warning: Attempt to present <AddPhotoViewController: 0x1e183fc0> on <UINavigationController: 0x1d594a70> whose view is not in the window hierarchy!

UINavigationController在我关闭视图后,有什么方法可以在视图顶部添加模态UIImagePickerController视图吗?

4

5 回答 5

2

在呈现另一个控制器之前,首先关闭当前模态控制器。IE

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
          AddPhotoViewController *apvc = [self.storyboard instantiateViewControllerWithIdentifier:@"addPhoto"];
          apvc.info = info;
          [self dismissViewControllerAnimated:YES completion:nil];

          [self presentViewController:apvc animated:YES completion:nil];
   }
于 2013-03-25T21:54:23.103 回答
1

类似于@nkongara 的答案,但通过使用完成块来避免任何奇怪的竞争条件,更加健壮。dismissViewControllerAnimated:completion此外,通过调用而[picker presentingViewController]不是假设它self始终是呈现视图控制器,代码更具前瞻性/防弹性。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    AddPhotoViewController *apvc = [self.storyboard instantiateViewControllerWithIdentifier:@"addPhoto"];
    apvc.info = info;
    [[picker presentingViewController] dismissViewControllerAnimated:YES completion:^{
        [self presentViewController:apvc animated:YES completion:nil];
    }];
}
于 2013-03-25T22:23:07.817 回答
1

你为什么不使用segue?在故事板中设置你的 segue,当从 imagepickercontroller 返回时,只需从代码中触发序列。

您可以参考 seque 的http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomSegues/CreatingCustomSegues.html

于 2013-03-25T20:49:50.270 回答
1

借助 unwind segue 的魔力,您可以获得所需的导航...

在你的第一个 viewController 中,实现一个 unwind segue

- (IBAction)unwindSegue:(UIStoryboardSegue*)segue
{
    NSLog (@"unwound");
       //you can extract data from your "addPhoto" viewController here
       //via segue.sourceViewController
}

一定要在你的头文件中声明它。

在您的“addPhoto”视图控制器中,添加某种关闭按钮,然后按住 CTRL 键拖动到控制器底部的“退出”符号。unwind segue 应该是可选的。

要到达addPhoto,您可以以模态方式呈现它 - 但要求 imagePickerController 进行呈现:

- (void) imagePickerController:(UIImagePickerController *)picker
 didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIViewController* addPhotoController = 
                [self.storyboard instantiateViewControllerWithIdentifier:@"addPhoto"];

    addPhotoController.infoDict = info; //passing the image data to addPhoto

    [picker presentViewController:addPhotoController 
                         animated:YES 
                       completion:nil];
}

所以你确实得到了两层呈现,但在每种情况下,都是前一个控制器进行呈现。DON"T 关闭 imagePickerController。当 unwind segue 执行时,它会为你处理关闭。

于 2013-03-26T00:48:50.920 回答
0

正如其他人在这里提到的,不可能在当前模式视图和父视图之间添加第二个模式视图。我解决了我的问题,但它更像是一个黑客而不是真正的解决方案。如果有人想做类似的事情,这是解释的链接:http: //xissburg.com/presenting-multiple-modal-view-controllers-at-once/

于 2013-03-25T23:46:32.053 回答