Application tried to present modally an active controller ios

瘦欲@ 提交于 2019-12-13 13:12:47

问题


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!


回答1:


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




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/19895049/application-tried-to-present-modally-an-active-controller-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!