Suc*_*chi 43 iphone uiviewcontroller ios
我使用以下代码检索一些消息并将它们放入我的收件箱.
MyInboxVC *inboxVC=[MyInboxVC get ];
//upload all the pending messages
UINavigationController *devNavController=[[MyappMgr get]getDeveloperNavigationController ];
[devNavController pushViewController:inboxVC animated:YES];
[devNavController setNavigationBarHidden:NO];
Run Code Online (Sandbox Code Playgroud)
我得到了例外
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported (<MyInboxVC: 0x1452a0>)'
Run Code Online (Sandbox Code Playgroud)
这是什么意思?我究竟做错了什么?
Mel*_*vin 72
我相信当你做一些非常快的动作时,这也会发生.我像这样建立一些东西:
if(![self.navigationController.topViewController isKindOfClass:[YOURCLASS class]]) {
Run Code Online (Sandbox Code Playgroud)
编辑//
这种情况更常发生在动画速度较慢的旧设备上,如iphone 3或3gs
bri*_*ear 20
首先处理崩溃,所以它不会杀死你的应用程序:
@try {
[self.navController pushViewController:viewController animated:NO];
} @catch (NSException * e) {
NSLog(@"Exception: %@", e);
} @finally {
//NSLog(@"finally");
}
Run Code Online (Sandbox Code Playgroud)
然后,如果你得到错误,请使用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");
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着ViewController返回[MyInboxVC get]的已经在导航堆栈中devNavController.您不能多次将相同的对象添加到堆栈.
显然,你之前已经MyInboxVC推过了.确保在不再需要时将其弹出.
这就是"它是什么意思"的答案,但没有足够的信息来知道你需要做些什么来解决它.
我的猜测是你的导航堆栈比你想象的要大得多,这意味着你不会像你想象的那样经常弹出.
这意味着您要推送相同的viewcontroller对象,以使其已经在其中。
[self.navigationController pushViewController:viewControllerObj animated:NO];
[self.navigationController pushViewController:viewControllerObj animated:NO];
Run Code Online (Sandbox Code Playgroud)
检查您是否将您的代码推入了循环,还是您不小心将代码放置了不止一次。
你是作为segue的一部分表演的吗?如果是,则无需将VC推送到导航控制器,因为segue已经执行此操作.这就是您的错误发生的原因 - 您正在推送已经在NavController堆栈上的VC.