不支持多次推送View Controller?

时间:2011-09-11 18:22:27

标签: ios error-handling crash viewcontroller

每当我尝试切换这样的视图时,我都会收到以下错误:

-(void)changeView1ToView4 {
    [self.navigationController1 pushViewController:view4 animated:YES];
}

当应用程序首次加载并且用户直接进入此视图时,不会发生这种情况。只有当我转到其他视图,返回主菜单,然后尝试转到此视图时,才会发生此崩溃。

此外,如果你不确定,我正在使用UINavigationController。此代码也在app委托中,我从具有父视图的视图控制器调用它,因此我使用.reference来调用它:

[self.reference changeView1ToView4];

有没有什么方法可以解决这个问题?

谢谢!

EDIT1:

[self.navigationController1 pushViewController:view4 animated:NO];
[self.navigationController1 pushViewController:view4 animated:YES];

I tried that and got this crash message in the console:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported (<View2: 0x18d540>)'

4 个答案:

答案 0 :(得分:8)

将2个视图推入堆栈时,请尝试调用:

[self.navigationController1 pushViewController:view4 animated:YES];

[self.navigationController1 pushViewController://secondviewcontrollername// animated:NO];

如果您尝试在两者上将animated:字段设置为YES的情况下推送多个视图,则会混淆堆栈。仅一次为一个视图设置动画。

答案 1 :(得分:5)

只是一个FYI,如果你调用setViewControllers:animated:没有必要调用pushViewController:之后,否则你会得到“不支持多次推送相同的视图控制器实例”崩溃。

答案 2 :(得分:0)

            @try {
               [self.navController pushViewController:viewController animated:NO];
            } @catch (NSException * e) {
                NSLog(@"Exception: %@", e);
                 [self.navigationController popToViewController:viewController animated:NO];
            } @finally {
                //NSLog(@"finally");
            }

答案 3 :(得分:0)

在按

之前检查此条件
if (![[self.navigationController topViewController] isKindOfClass:[YOURCONTROLLER class]])
    [self.navigationController pushViewController:YOURCONTROLLER animated:YES];