尝试在视图不在窗口层次结构中的VC上显示模态VC

时间:2013-05-09 14:24:27

标签: objective-c uinavigationcontroller uiimagepickercontroller modalviewcontroller segue

我的故事板看起来像这样:

Storyboard

这是它的工作原理:InitialSlidingViewController在其.m文件中将导航VC实例化为topViewController,如下所示:

 -(void)viewDidLoad {
  [super viewDidLoad];

  self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Navigation"];

}

导航VC实例化后,它会创建基础菜单并将其设置为underLeftVC。这是使用ECSlidingViewController。这会创建一个使用平移手势的滑动菜单,类似于Facebook。

NavigationVC的rootVC是我的ItemsCDTVC。当我点击+按钮时,它会弹出一个动作表,允许我从相机拍摄照片或从照片库中选择。但是,我希望它以模态方式转换为ManageItemVC并设置我刚刚选择/拍摄到UIImageView的图片。当我尝试呈现时,它会抛出以下错误:

Warning: Attempt to present <ManageItemViewController: 0x81d52a0> on <InitialSlidingViewController: 0x8122ce0> whose view is not in the window hierarchy!

这是真的,因为InitialSlidingVC不在屏幕上,ItemCDTVC是,但我该如何解决这个问题?

我在我的故事板中设置了模态类型segue,其中包含来自底部灵活空间的标识符(当点击时,它没有做任何事情,因此我选择它来创建视觉segue)。这是我在ItemCDTVC中的segue代码:

 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // let's get the edited image
    UIImage *image = info[UIImagePickerControllerEditedImage];
    // but if for some reason we disable editing in the future, this is our safeguard
    if(!image) image = info[UIImagePickerControllerOriginalImage];
    if(image) {
        self.pictureImageView.image = image;
        [self performSegueWithIdentifier:@"addItemSegue" sender:self];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"addItemSegue"]){
        ManageItemViewController *manageItemVC = (ManageItemViewController *)[segue destinationViewController];
        manageItemVC.pictureImageView = self.pictureImageView;
    }
}

尝试呈现模态VC时失败。

1 个答案:

答案 0 :(得分:5)

修正了,我唯一要做的就是在尝试执行segue之前解除imagePickerController,如下所示:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // let's get the edited image
    UIImage *image = info[UIImagePickerControllerEditedImage];
    // but if for some reason we disable editing in the future, this is our safeguard
    if(!image) image = info[UIImagePickerControllerOriginalImage];
    if(image) {
        self.pictureImage = image;
        [self dismissViewControllerAnimated:NO completion:^{
            [self performSegueWithIdentifier:@"addItemSegue" sender:self];
        }];
    }
}