candidaMan candidaMan - 1 year ago 155
iOS Question

Pop 2 view controllers in Nav Controller in Swift

I have found many ways to pop back 2 view controllers in a navigation controllers using object 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 view controllers?
Any guidance would be appreciated

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download