应用程序尝试以模式方式显示活动控制器ios

5 objective-c viewcontroller modalviewcontroller ios segue

我试图用父视图控制器设置ViewController,以显示它可以提供回调,我使用 PrepareForSegue

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

它崩溃了,给我错误信息: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller.

所以我尝试使用另一种方法,并在触摸按钮时设置了新的视图控制器,

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

    [self presentViewController:newController animated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)

但是没有运气,它仍然给我同样的错误信息,有人可以请教吗?谢谢!

小智 4

解决了这个问题,因为父视图控制器是一个tableViewController,它被嵌入到一个 中navigationViewController。这就是为什么应该推动 Segue 而不是执行模式转换。