其他解决方案:Pushing the same view controller instance more than once is not supported

导语:

这个报错出现,几乎都是一种解决方案:在push之前加判断,确定没有重复的实例再push操作!如下:

if(![self.navigationController.topViewController isKindOfClass:[searchViewCon class]]) {
        [self.navigationController pushViewController:searchViewCon animated:YES];
    }

然鹅!鄙人遇到的两次都不是这么解决的,也不是这个原因,别问我为什么,我也不知道!(我就是要大写,泄愤)😢😢😢

问题场景1:

导入别人的工程到项目中后,运行时出现了报错:
Pushing the same view controller instance more than once is not supported
goggle了好久都没找到解决方案,这个问题其实写的很清楚,就是重复push了,尝试了做判断再push依旧报错;
没办法,看代码,发现有这段代码:

dispatch_async(dispatch_get_main_queue(), ^{
        ......
        [navigationController pushViewController:vc animated:YES];//做push操作
     });

解决方案:

在push操作前做了主线程处理,尝试性把dispatch_async去除掉,好了,==!。

总结:

我猜测应该是异步的问题,异步导致多次执行了push操作。

问题场景2:

一样是这个报错Pushing the same view controller instance more than once is not supported,看代码如下:

UINavigationController *nav = self.navigationController;
[self.navigationController popToRootViewControllerAnimated:NO];
[nav pushViewController:mvc animated:YES];

这段代码的意思是在弹出新页面之前把栈里面除了根界面的其他界面都pop出去;另外,在这段代码之前本人遍历过self.navigationController.viewControllers,发现里面并没有我需要push的实例,也就是说按道理不应该出现Pushing the same view controller instance more than once is not supported,可偏偏出现了!

解决方案:

[self.navigationController popToRootViewControllerAnimated:NO];注释掉,别问我为什么,我也不知道,依旧一脸懵逼;

备注:

为了达到pop出去的效果,我选择在弹出的新界面之后调用[self.navigationController popToRootViewControllerAnimated:NO];

总结:

这两次报错都很神奇,肯定是我作为一个小白没弄明白原理,鄙人无法解释啊, 两次瞎猫碰上死耗子,记录下,要是有大神能推断出原因希望能告知🙏🙏🙏。

后续:

1.后来找到这两个问题的原因了,设置导航根结点使用的是[[URNavigationController alloc] initWithRootViewController:self.conversationVC],注意!这里使用的是URNavigationController,这玩意是啥呢,是"前人"对UINavigationController的二次封装,里面已经“界面卡顿,点击过快,多次push同一个VC”的问题做了处理,因此,本人对push操作做的重复验证导致了报错;
2.至于为什么重复验证会导致这个问题,暂时还没找出,附上对UINavigationController二次封装的代码,其实就是重写了pushViewController方法:

//解决界面卡顿,点击过快,多次push同一个VC
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [super pushViewController:viewController animated:animated];
    [self setNavigationBarHidden:NO animated:YES];
    if (self.viewControllers.count > 0) {
//        viewController.hidesBottomBarWhenPushed = YES;
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[UIView new]];
    }
    if (![[super topViewController] isKindOfClass:[viewController class]]) {
        [super pushViewController:viewController animated:animated];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。