Tout d'abord, gérez le crash pour qu'il ne tue pas votre application :
@try {
[self.navController pushViewController:viewController animated:NO];
} @catch (NSException * e) {
NSLog(@"Exception: %@", e);
} @finally {
//NSLog(@"finally");
}
Si vous obtenez l'erreur, utilisez 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");
}
}