如何检查控制器是否已经在navigationcontroller viewcontrollers堆栈上?

时间:2021-08-02 20:35:27

i'm getting this error:

我收到这个错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported

How does one check if a controller already exists in the stack and not push that controller but move to it?

如何检查控制器中是否已存在控制器而不是推动该控制器而是移动到控制器?

Here's some code where I'm pushing a controller based on a tab selection:

这是我根据选项卡选择推送控制器的一些代码:

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
    }
}

2 个解决方案

#1


3  

It seems like you are pushing a same controller to your navigation stack. You can't push a view controller onto the stack that already exists in the stack. It is probably that you call you tabSelected() method multiple times, so you should make sure it doesnt get called multiple times.

您似乎正在将同一个控制器推送到导航堆栈。您无法将视图控制器推送到堆栈中已存在的堆栈。你可能多次调用tabSelected()方法,所以你应该确保它不会被多次调用。

A good practice to prevent your crash is to pop off the existing controller, which already in the stack. So you should do something like self.navigationController?.popToViewController(myViewController, animated: true) whenever you leave your view controller.

防止崩溃的一个好方法是弹出已经在堆栈中的现有控制器。因此,每当您离开视图控制器时,您应该执行类似self.navigationController?.popToViewController(myViewController,animated:true)的操作。

Or you can do the following to check whatever your controller is already in the stack:

或者您可以执行以下操作来检查堆栈中已有的控制器:

 if (self.navigationController?.topViewController.isKindOfClass(ViewController) != nil) {

}

For your particular case, do the following:

对于您的特定情况,请执行以下操作:

if(mainNavigationController.topViewController.isKindOfClass(ProfileViewController) != nil) {

    }

#2


4  

You can check the navigation controller's viewControllers property.

您可以检查导航控制器的viewControllers属性。

if contains(mainNavigationController.viewControllers, controller) {
  // move it
} else {
  // push it
}

#1


3  

It seems like you are pushing a same controller to your navigation stack. You can't push a view controller onto the stack that already exists in the stack. It is probably that you call you tabSelected() method multiple times, so you should make sure it doesnt get called multiple times.

您似乎正在将同一个控制器推送到导航堆栈。您无法将视图控制器推送到堆栈中已存在的堆栈。你可能多次调用tabSelected()方法,所以你应该确保它不会被多次调用。

A good practice to prevent your crash is to pop off the existing controller, which already in the stack. So you should do something like self.navigationController?.popToViewController(myViewController, animated: true) whenever you leave your view controller.

防止崩溃的一个好方法是弹出已经在堆栈中的现有控制器。因此,每当您离开视图控制器时,您应该执行类似self.navigationController?.popToViewController(myViewController,animated:true)的操作。

Or you can do the following to check whatever your controller is already in the stack:

或者您可以执行以下操作来检查堆栈中已有的控制器:

 if (self.navigationController?.topViewController.isKindOfClass(ViewController) != nil) {

}

For your particular case, do the following:

对于您的特定情况,请执行以下操作:

if(mainNavigationController.topViewController.isKindOfClass(ProfileViewController) != nil) {

    }

#2


4  

You can check the navigation controller's viewControllers property.

您可以检查导航控制器的viewControllers属性。

if contains(mainNavigationController.viewControllers, controller) {
  // move it
} else {
  // push it
}