Abhishek Ravi
Abhishek Ravi

Reputation: 137

Dismissing a view controller and opening another one once complete, giving error

I process some photos and once thats done, I call the following function to show the output UI. I need to ensure that the current capture view controller is dismissed and don't want it running in the background. The function I use is this:

-(void)openOutputUIWithImage:(UIImage*)displayImage{ //update UI with new viewcontroller with specified image to display
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    outputView = [mainStoryboard instantiateViewControllerWithIdentifier:@"OutputView"];
    outputView.img = displayImage;
    [self dismissViewControllerAnimated:NO completion:^{
        [self presentViewController:outputView animated:YES completion:^{
            NSLog(@"output view opened!");
        }];
    }];
}

But I'm getting the following error:

Warning: Attempt to present <OutputViewController: 0x100858af0> on <CircleDetectionViewController: 0x100857c20> whose view is not in the window hierarchy!

I'm really not sure what I'm doing wrong, I haven't had much experience working with multiple view controllers. It seems to hit my viewDidLoad method in my other view controller but just displays a blank screen. Any help would be really appreciated! Thanks in advance!

Upvotes: 0

Views: 49

Answers (3)

Abhishek Ravi
Abhishek Ravi

Reputation: 137

This works:

UIViewController* presentingViewController = self.presentingViewController;
    [self dismissViewControllerAnimated:YES completion:^{
        [presentingViewController presentViewController:outputView animated:YES completion:nil];

    }];

Referenced this answer: https://stackoverflow.com/a/33058529/5071756

Upvotes: 1

Subramanian P
Subramanian P

Reputation: 4375

First unnecessarily allocating OutputViewController and getting the OutputViewController again from the story board. So OutputViewController* outputView = [[OutputViewController alloc] init]; line of code is not required.

You are trying to open a OutputViewController inside the completion block of the CircleDetectionViewController dismissViewController method. You are trying to open a OutputViewController on top of the CircleDetectionViewController. But your dismissing the CircleDetectionViewController that is wrong.

Get the previous view controller of the CircleDetectionViewController and present the OutputViewController from that and dismiss the CircleDetectionViewController.

Update 1:

First present the outputView

[self.presentingViewController presentViewController:outputView animated:YES completion:nil];

And dismiss the CircleDetectionView

[self dismissViewControllerAnimated:NO completion:nil];

Upvotes: 0

Dima
Dima

Reputation: 23634

Who is self in this code? Based on the error, self is your CircleDetectionViewController. That means what's happening here is that dismissViewControllerAnimated causes the calling VC to dismiss itself (so that it is not in the view hierarchy anymore, and then tries to present a view controller on top of itself even though it has just been dismissed.

2 possible fixes come to mind:

  1. Present your OutputViewController on top of your CircleDetectionViewController without dismissing it first.

  2. Handle the logic for dismissing/presenting outside of these presented view controllers, in whatever the parent view controller is.

Upvotes: 0

Related Questions