Pop 2 view controllers in Nav Controller in Swift

2019-03-09 00:48发布

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 doesn't seem to be working.

What would be the best approach to pop back to UIViewController? Any guidance would be appreciated Thanks

6条回答
【Aperson】
2楼-- · 2019-03-09 01:09

You can dynamically pop multiple viewcontrollers in (swift 2.0)

let allViewController: [UIViewController] = self.navigationController!.viewControllers as [UIViewController];

                        for aviewcontroller : UIViewController in allViewController
                        {
                            if aviewcontroller .isKindOfClass(YourDestinationViewControllerName)
                            {
                             self.navigationController?.popToViewController(aviewcontroller, animated: true)
                            }
                        }
查看更多
太酷不给撩
3楼-- · 2019-03-09 01:11

I wrote an UIViewController extension (Swift 3+ ready)

You could use like this :

/// pop back n viewcontroller
func popBack(_ nb: Int) {
    if let viewControllers: [UIViewController] = self.navigationController?.viewControllers {
        guard viewControllers.count < nb else {
            self.navigationController?.popToViewController(viewControllers[viewControllers.count - nb], animated: true)
            return
        }
    }
}

Usage :

self.popBack(3)

Bonus dismiss to a specific viewcontroller

/// pop back to specific viewcontroller
func popBack<T: UIViewController>(toControllerType: T.Type) {
    if var viewControllers: [UIViewController] = self.navigationController?.viewControllers {
        viewControllers = viewControllers.reversed()
        for currentViewController in viewControllers {
            if currentViewController .isKind(of: toControllerType) {
                self.navigationController?.popToViewController(currentViewController, animated: true)
                break
            }
        }
    }
}

Usage :

self.popBack(toControllerType: MyViewController.self)
查看更多
看我几分像从前
4楼-- · 2019-03-09 01:18

Expanding on my comment, find the second last view controller in the viewControllers array and then use popToViewController to avoid overwriting the entire view controller stack.

Example (assumes the navigation controller has more than 1 view controller):

func backTwo() {
    let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController]
    self.navigationController!.popToViewController(viewControllers[viewControllers.count - 3], animated: true)
}
查看更多
Summer. ? 凉城
5楼-- · 2019-03-09 01:18

user5320485 answer in swift3

let viewControllers = self.navigationController!.viewControllers as [UIViewController];
for aViewController:UIViewController in viewControllers {
    if aViewController.isKind(of: AdCreateViewController.self) {
        _ = self.navigationController?.popToViewController(aViewController, animated: true)
    }
}
查看更多
【Aperson】
6楼-- · 2019-03-09 01:23

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<T: UIViewController>(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
            }
        }
    }

 }
查看更多
乱世女痞
7楼-- · 2019-03-09 01:31

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)

查看更多
登录 后发表回答