Pop 2 view controllers in Nav Controller in Swift

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

    I did a helper to make it easy:

    import Foundation
    import UIKit
    
    class NavigationHelper {
    
        static func back(number: Int, from viewControlller : UIViewController) {
            let viewsBack = number + 1
            let viewControllers: [UIViewController] = viewControlller.navigationController!.viewControllers as [UIViewController]
            viewControlller.navigationController!.popToViewController(viewControllers[viewControllers.count - viewsBack], animated: true)
        }
    
        static func back(number: Int, from viewControlller: UITableViewController) {
            let viewsBack = number + 1
            let viewControllers: [UIViewController] = viewControlller.navigationController!.viewControllers as [UIViewController]
            viewControlller.navigationController!.popToViewController(viewControllers[viewControllers.count - viewsBack], animated: true)
        }
    }
    

    For example, in your ViewController you can use NavigationHelper.back(number: 1, from: self)

提交回复
热议问题