Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pop 2 view controllers in Nav Controller in Swift

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

like image 592
candidaMan Avatar asked Oct 01 '14 01:10

candidaMan


People also ask

How do I add a view controller to my navigation controller?

Step 1: Embed root view controller inside a navigation controller. In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .


3 Answers

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)
}

Objective-C

NSArray *viewControllers = [self.navigationController viewControllers];
[self.navigationController popToViewController:viewControllers[viewControllers.count - 3] animated:YES];
like image 142
Aaron Wojnowski Avatar answered Oct 09 '22 17:10

Aaron Wojnowski


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)
like image 23
Maximelc Avatar answered Oct 09 '22 18:10

Maximelc


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)
    }
}
like image 13
Nidhin Avatar answered Oct 09 '22 17:10

Nidhin