我收到这个错误: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported 如何检查堆栈中是否已存在控制器而不是推动该
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported
如何检查堆栈中是否已存在控制器而不是推动该控制器而是移动到控制器?
这是我根据选项卡选择推送控制器的一些代码:
func tabSelected(tab: String) { switch tab{ case "payment": mainNavigationController.popToViewController(myAccountViewController, animated: true) break case "delivery": mainNavigationController.pushViewController(deliveryViewController, animated: true) break case "service": mainNavigationController.pushViewController(serviceViewController, animated: true) break case "profile": mainNavigationController.pushViewController(profileViewController, animated: true) break default: break } }您似乎正在将同一个控制器推送到导航堆栈.您无法将视图控制器推送到堆栈中已存在的堆栈.你可能多次调用tabSelected()方法,所以你应该确保它不会被多次调用.
防止崩溃的一个好方法是弹出现有的控制器,
已经在堆栈中.因此,每当您离开视图控制器时,您应该执行类似self.navigationController?.popToViewController(myViewController,animated:true)的操作.
或者您可以执行以下操作来检查控制器已在堆栈中的任何内容:
if (self.navigationController?.topViewController.isKindOfClass(ViewController) != nil) { }
对于您的特定情况,请执行以下操作:
if(mainNavigationController.topViewController.isKindOfClass(ProfileViewController) != nil) { }