Pop 2 view controllers in Nav Controller in Swift

后端 未结 8 1019
星月不相逢
星月不相逢 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

    Swift 4 UINavigationController extension version of @Maximelc's answer:

    extension UINavigationController {
    
        func popBack(_ nb: Int) {
            let viewControllers: [UIViewController] = self.viewControllers
            guard viewControllers.count < nb else {
                self.popToViewController(viewControllers[viewControllers.count - nb], animated: true)
                return
            }
        }
    
        /// pop back to specific viewcontroller
        func popBack(toControllerType: T.Type) {
            var viewControllers: [UIViewController] = self.viewControllers
            viewControllers = viewControllers.reversed()
            for currentViewController in viewControllers {
                if currentViewController .isKind(of: toControllerType) {
                    self.popToViewController(currentViewController, animated: true)
                    break
                }
            }
        }
    
     }
    

提交回复
热议问题