5

I was trying to set the ViewController with a parent view controller before it shows show that it can provide call backs, I done this using PrepareForSegue

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"newQuarter"])
    {
        [segue.destinationViewController setParentViewController:self];
    }
}

It crashed giving me the error message: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller.

So I tried using another method and set up a new view controller on the button touches up,

- (IBAction) buttonClicked
{
    NewViewController *newController = [[NewViewController alloc] init];
    [newController setParentViewController:self];

    [self presentViewController:newController animated:YES completion:nil];
}

but with no luck it is still giving me the same error message, can anyone please advice? Thanks!

  • possible duplicate of ["Application tried to present modally an active controller"?](http://stackoverflow.com/questions/7429014/application-tried-to-present-modally-an-active-controller) – Carl Veazey Nov 10 '13 at 21:54
  • 1
    @CarlVeazey this is not a duplicate of "Application tried to present modally an active controller". Thank you for suggesting though! :D –  Nov 10 '13 at 22:22

3 Answers3

4

Resolved the problem, since the parent view controller is a tableViewController, which it was embedded in a navigationViewController. That's why the segue should be pushed rather then performing modal transition.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
3

I had the same issue and Matthew's explanation seems correct.

Replace:

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

with:

[self.navigationController pushViewController:newController animated:YES];
Sebastian Dwornik
  • 2,526
  • 2
  • 34
  • 57
2

This line:

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

will perform a MODAL segue, which is what gives the error.

Using this line instead:

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

performs a segue by 'PUSHING' a new view controller onto the Navigation Controller stack (in XCode 6 and above, this is the same thing as defining a segue type of 'show' on the storyboard). This is why you need this when you're using a Navigation Controller.

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206