How can I pop specific View Controller in Swift

前端 未结 16 2823
一生所求
一生所求 2020-12-08 02:21

I used the Objective-C code below to pop a specific ViewController.

for (UIViewController *controller in self.navigationController.         


        
相关标签:
16条回答
  • 2020-12-08 02:59

    In latest swift

       @IBAction func popToConversationsVC(_ sender: UIButton) {
            if (self.navigationController != nil) {
                for vc in  self.navigationController!.viewControllers {
                    if vc is ConversationsVC {
                         self.navigationController?.popToViewController(vc, animated: false)
                    }
                }
                }
        }
    
    0 讨论(0)
  • 2020-12-08 03:00

    For Swift 3+

    let viewControllers: [UIViewController] = self.navigationController!.viewControllers
    for aViewController in viewControllers {
        if aViewController is YourViewController {
            self.navigationController!.popToViewController(aViewController, animated: true)
        }
    }
    
    0 讨论(0)
  • 2020-12-08 03:00

    swift5

    let controllers : Array = self.navigationController!.viewControllers
    self.navigationController!.popToViewController(controllers[1], animated: true)
    
    0 讨论(0)
  • 2020-12-08 03:00
    extension UINavigationController {
        func popBack(to vc: AnyClass, animated: Bool = true) {
            guard let elementFound = (viewControllers.filter { $0.isKind(of: vc) }).first else {
                fatalError("cannot pop back to \(vc) as it is not in the view hierarchy")
            }
            self.popToViewController(elementFound, animated: animated)
        }
    }
    
    0 讨论(0)
提交回复
热议问题