应用程序试图以模态方式呈现一个主动控制器 ios

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19895049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:50:45  来源:igfitidea点击:

Application tried to present modally an active controller ios

iosobjective-cseguemodalviewcontrollerviewcontroller

提问by

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

我试图在显示它可以提供回调之前使用父视图控制器设置 ViewController,我使用 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.

它崩溃了,给我错误消息: 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!

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

采纳答案by

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.

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

回答by Sebastian Dwornik

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

回答by Chris Halcrow

This line:

这一行:

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

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

将执行 MODAL segue,这就是导致错误的原因。

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.

通过将新的视图控制器“推”到导航控制器堆栈上来执行转场(在 XCode 6 及更高版本中,这与在情节提要上定义“显示”的转场类型相同)。这就是为什么在使用导航控制器时需要它的原因。