如何以编程方式在UINavigationController中显示第二个模态ViewController

时间:2013-03-25 20:36:33

标签: ios objective-c xcode ios6 uinavigationcontroller

在我的UINavigationController个观看次数中,用户可以按“添加”按钮,使用UIImagePickerController从相册中选择照片。当用户选择所需照片时,我想将他移至AddPhotoViewController视图,在那里他可以添加标题和说明,而不是按“保存”按钮保存数据,取消AddPhotoViewController视图并返回上一个{{ 1}}查看。到目前为止,我只能推动UINavigationController视图,我不能将其作为模态视图。这就是我这样做的方式:

AddPhotoViewController

上面的代码工作正常,但我想添加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]; } ,但在iOS 6.0中已经过折旧。比我尝试过:

presentModalViewController:animated:

而不是:

   [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视图的顶部添加模态视图?

5 个答案:

答案 0 :(得分:2)

首先关闭当前模态控制器,然后再显示另一个控制器。即

    -(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];
   }

答案 1 :(得分:1)

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

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

答案 2 :(得分: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];
    }];
}

答案 3 :(得分:1)

由于放松赛道的魔力,你可以获得你想要的导航......

在第一个viewController中,实现一个展开segue

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

请务必在头文件中声明它。

在“addPhoto”viewController中,添加某种类型的消除按钮,然后按住CTRL键拖动到控制器底部的“退出”符号。展开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时,它将为你解雇。

答案 4 :(得分:0)

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