ios “不支持多次推送同一个视图控制器实例”异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7083124/
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
"Pushing the same view controller instance more than once is not supported" exception
提问by Suchi
I am using the following code to retrieve some messages and putting them into my inbox.
我正在使用以下代码检索一些邮件并将它们放入我的收件箱。
MyInboxVC *inboxVC=[MyInboxVC get ];
//upload all the pending messages
UINavigationController *devNavController=[[MyappMgr get]getDeveloperNavigationController ];
[devNavController pushViewController:inboxVC animated:YES];
[devNavController setNavigationBarHidden:NO];
I get the exception
我得到了例外
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported (<MyInboxVC: 0x1452a0>)'
What does it mean? What am I doing wrong?
这是什么意思?我究竟做错了什么?
回答by Melvin
I believe when you do some actions really fast this can happens too. I build something in like this:
我相信当你非常快速地做一些动作时,这也会发生。我建立了这样的东西:
if(![self.navigationController.topViewController isKindOfClass:[YOURCLASS class]]) {
回答by brian.clear
Firstly handle the crash so it doesnt kill your app:
首先处理崩溃,这样它就不会杀死您的应用程序:
@try {
[self.navController pushViewController:viewController animated:NO];
} @catch (NSException * e) {
NSLog(@"Exception: %@", e);
} @finally {
//NSLog(@"finally");
}
Then if you get the error use popTo
然后,如果您收到错误,请使用 popTo
- (void)pushViewController:(UIViewController *)viewController {
if (viewController) {
@try {
[self.navController pushViewController:viewController animated:NO];
} @catch (NSException * ex) {
//“Pushing the same view controller instance more than once is not supported”
//NSInvalidArgumentException
NSLog(@"Exception: [%@]:%@",[ex class], ex );
NSLog(@"ex.name:'%@'", ex.name);
NSLog(@"ex.reason:'%@'", ex.reason);
//Full error includes class pointer address so only care if it starts with this error
NSRange range = [ex.reason rangeOfString:@"Pushing the same view controller instance more than once is not supported"];
if ([ex.name isEqualToString:@"NSInvalidArgumentException"] &&
range.location != NSNotFound) {
//view controller already exists in the stack - just pop back to it
[self.navController popToViewController:viewController animated:NO];
} else {
NSLog(@"ERROR:UNHANDLED EXCEPTION TYPE:%@", ex);
}
} @finally {
//NSLog(@"finally");
}
} else {
NSLog(@"ERROR:pushViewController: viewController is nil");
}
}
回答by MarkPowell
It means that the ViewController
returned from [MyInboxVC get]
is already in the navigation stack of devNavController
. You can not add the same object to the stack multiple times.
这意味着从ViewController
返回[MyInboxVC get]
的已经在 的导航堆栈中devNavController
。您不能将同一个对象多次添加到堆栈中。
Apparently, you already have a MyInboxVC
pushed earlier. Insure that you've popped it when it was no longer needed.
显然,您之前已经MyInboxVC
推送过了。确保在不再需要时将其弹出。
That's the "what's it mean" answer, but don't have enough info to know what you need to do to fix it.
这就是“这是什么意思”的答案,但没有足够的信息来知道您需要做什么来修复它。
My guess is your Navigation Stack is growing larger than you are expecting, meaning you are not popping as often as you should.
我的猜测是你的导航堆栈比你预期的要大,这意味着你没有像你应该的那样经常弹出。
回答by micnguyen
Are you performing this as part of a segue? If you are, there is no need to push a VC onto your Navigation Controller because the segue will do it already. That is why your error is occurring - you are pushing a VC that is already on the stack of the NavController.
您是否将其作为 segue 的一部分执行?如果是,则无需将 VC 推送到您的导航控制器上,因为 segue 已经完成了。这就是发生错误的原因 - 您正在推送一个已经在 NavController 堆栈上的 VC。
回答by Durai Amuthan.H
It means you are pushing the same viewcontroller object to stack again when it's already in there.
这意味着当它已经在那里时,您正在将相同的视图控制器对象再次推送到堆栈。
[self.navigationController pushViewController:viewControllerObj animated:NO];
[self.navigationController pushViewController:viewControllerObj animated:NO];
check if u r pushing inside a loop or if u've accidentally placed the code more than one time..
检查您是否在循环内推动,或者您是否不小心将代码放置了不止一次..
回答by Elie
The Main Reason for this problem, obviously if the code that pushed the view controller is called more than once. This could occur for many reasons, most common mistake when a callback method is triggered from a background thread, where this method could be executed more than once while it is still pushing the view controller. Example: Calling a service api on background thread when tapping a button, which will allow you to press the button more than once, and therefore the callback which pushes the view controller get called more than once. @Melvin and @Sam solution is valid as long as you do not want to fix the original problem by not pushing more than once.
这个问题的主要原因,很明显,如果推送视图控制器的代码被调用不止一次。这可能有多种原因,最常见的错误是从后台线程触发回调方法时,该方法可能会在它仍在推送视图控制器时多次执行。示例:点击按钮时在后台线程上调用服务 api,这将允许您多次按下按钮,因此推动视图控制器的回调被调用多次。只要您不想通过不多次推送来解决原始问题,@Melvin 和 @Sam 解决方案就是有效的。
回答by pulse4life
This was happening to me on a bar button click happening too fast, and was hard to reproduce, unless you went nuts on the button taps. The following fixed it by disabling the the button, starting the nav push, then enabling the button on the main thread (because it would be called after animation from the push occurred).
这发生在我身上,因为一个条形按钮点击发生得太快了,而且很难重现,除非你对按钮的点击发疯了。下面通过禁用按钮,启动导航推送,然后在主线程上启用按钮来修复它(因为它将在推送动画后被调用)。
- (void)showMore
{
self.navigationItem.leftBarButtonItem.enabled = NO;
[self.navigationController pushViewController:moreVC animated:YES];
[self.navigationItem.leftBarButtonItem performSelectorOnMainThread:@selector(setEnabled:) withObject:@(YES) waitUntilDone:NO];
}
回答by Kavish
Make sure you are not adding the view controller twice in the navigation stack. Eg - in below example self.mainViewC is pushed twice because it is initially instantiated in the navController, and is then pushed onto the navController again in the last line, which would cause this issue.
确保您没有在导航堆栈中两次添加视图控制器。例如 - 在下面的例子中 self.mainViewC 被推送了两次,因为它最初是在 navController 中实例化的,然后在最后一行再次推送到 navController 上,这会导致这个问题。
navController=[[UINavigationController alloc] initWithRootViewController:self.mainViewC];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
[navController pushViewController:self.mainViewC animated:NO];
In this case mainViewC has already been added to stack when initWithRootViewController was written. There is no need of pushViewController again.
在这种情况下,在编写 initWithRootViewController 时,mainViewC 已经被添加到堆栈中。不再需要 pushViewController。
回答by Sivaprasad
This is an expected behavior of UINavigationController where an exception is thrown when trying to push a view controller which is already present in the stack (Its there from iOS 2.2).
这是 UINavigationController 的预期行为,其中在尝试推送已经存在于堆栈中的视图控制器时抛出异常(它来自 iOS 2.2)。
回答by arcady bob
Another option that I have experienced is that [MyInboxVC get ] is not returning an instance of a MyInboxVC object at all. A tell tale sign of this would be that the error is saying 'Pushing the same view controller instance more than once is not supported (notTheInboxVC: 0x9e31660)' ie. the class being pushed more than once is not the MyInboxVC expected (a fall through from MyInboxVC not being allocated)
我遇到的另一个选择是 [MyInboxVC get ] 根本不返回 MyInboxVC 对象的实例。一个明显的迹象是错误是说“不支持多次推送相同的视图控制器实例(notTheInboxVC:0x9e31660)”,即。多次推送的类不是 MyInboxVC 预期的(未分配 MyInboxVC 的失败)