Pop 2 view controllers in Nav Controller in Swift

后端 未结 8 1006
星月不相逢
星月不相逢 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: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)
        }
    }
    

提交回复
热议问题