Pop 2 view controllers in Nav Controller in Swift

后端 未结 8 1003
星月不相逢
星月不相逢 2021-01-30 12:46

I have found many ways to pop back 2 UIViewControllers in UINavigationController using Objective-C, however when I try and switch that over to Swift it

相关标签:
8条回答
  • 2021-01-30 13:43
     let allViewControllers : [UIViewController] = self.navigationController!.viewControllers as [UIViewController]
            self.navigationController?.popToViewController(allViewControllers[allViewControllers.count-3], animated: true)
    
    0 讨论(0)
  • 2021-01-30 13:44

    Here is another, slightly "fool-proof" version:

    extension UINavigationController {
        func popBack(_ count: Int) {
            guard count > 0 else {
                return assertionFailure("Count can not be a negative value.")
            }
            let index = viewControllers.count - count - 1
            guard index > 0 else {
                return assertionFailure("Not enough View Controllers on the navigation stack.")
            }
            popToViewController(viewControllers[index], animated: true)
        }
    }
    
    0 讨论(0)
提交回复
热议问题