ios 如何快速将视图控制器弹出到以前的视图控制器之一?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26343546/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:10:02  来源:igfitidea点击:

How to pop view controller to one of the previous view controllers in swift?

iosiphoneswift

提问by serhanaksut

I have been trying to pop my view controller to one of previous view controllers in my view stack. Assume that, There are firstVC, secondVC, thirdVC and fourthVCviewcontrollers in my view stack. The current view controller is fourth one, and there is a tableview in fourthVC. If user delete all the rows in tableview, I should direct the user to secondVC. I had an idea that I would create another navigationcontroller and present it with presentViewControllercommand. However, this is not a solution for my problem. Because I thougt that a navigation problem appears for this case. How can I find best solution for this case ?

我一直在尝试将我的视图控制器弹出到我的视图堆栈中以前的视图控制器之一。假设,在我的视图堆栈中有firstVC、secondVC、thirdVC 和第四个 VC视图控制器。当前视图控制器是第四个,在第四个VC中有一个tableview。如果用户删除 tableview 中的所有行,我应该将用户定向secondVC。我有一个想法,我将创建另一个导航控制器并使用presentViewController命令呈现它。但是,这不是我的问题的解决方案。因为我认为这种情况会出现导航问题。我怎样才能找到这种情况的最佳解决方案?

Thank you for your answers, Best regards

谢谢你的回答,最好的问候

回答by Aaron Wojnowski

Instead of doing a generic popViewControllerAnimated:call, use popToViewController:animated:. You could detect if the user has deleted all of the rows in which case, do something like this (otherwise just pop one view controller):

而不是做一个普通的popViewControllerAnimated:电话,使用popToViewController:animated:。在这种情况下,您可以检测用户是否删除了所有行,请执行以下操作(否则只需弹出一个视图控制器):

let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController];
self.navigationController!.popToViewController(viewControllers[viewControllers.count - 2], animated: true);

回答by BluGeni

If you want to pop to a specific view control and dont know the count to go back you can use this:

如果你想弹出到一个特定的视图控件并且不知道返回的计数,你可以使用这个:

 let viewControllers: [UIViewController] = self.navigationController!.viewControllers as! [UIViewController];

        for aViewController in viewControllers {
            if(aViewController is ViewControllerYouWantToGoTo){
                 self.navigationController!.popToViewController(aViewController, animated: true);
            }
        }

回答by Arvind Kumar

After lots of effort I have created swift extension of back to a particular view controller in Swift 3.0.

经过大量努力,我在 Swift 3.0 中创建了返回到特定视图控制器的快速扩展。

extension UINavigationController {

    func backToViewController(viewController: Swift.AnyClass) {

            for element in viewControllers as Array {
                if element.isKind(of: viewController) {
                    self.popToViewController(element, animated: true)
                break
            }
        }
    }
}

Method calling:

方法调用:

   self.navigationController?.backToViewController(viewController: BarCodeScannerVC.self)

回答by Azharhussain Shaikh

Here you can do this by the find the controller from the UINavigationController stack and with the help of for loop and check the condition of your desire controller and if the condition gets fulfilled it will pop to the destionation controller.

在这里,您可以通过从 UINavigationController 堆栈中找到控制器并在 for 循环的帮助下检查您想要的控制器的条件,如果条件得到满足,它将弹出到目标控制器。

let viewControllersStack: [UIViewController] = self.navigationController!.viewControllers
      for firstViewcontroller in viewControllersStack
      {
           if firstViewcontroller is desireViewController
           {
                self.navigationController!.popToViewController(firstViewcontroller, animated: true)
           }
      }