Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - How to popViewControllerAnimated two views back

Tags:

swift

swift2

I have my Storyboard with some UIViewControllers and my main UINavigationController.

Then I have a UIViewController where on tableView didSelectRowAtIndexPath I pop the UIViewController:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){

        let provider = self.results[indexPath.row]

        self.delegate?.didProviderSelected(provider)
        self.navigationController?.popViewControllerAnimated(true)
    }

This works fine because then I return to the previous view but what I need to return two views back.

How can I achieve this?

like image 950
VAAA Avatar asked Mar 15 '26 17:03

VAAA


1 Answers

You can use popToViewController(_:animated:) to pop previous two views:

if let viewControllers = self.navigationController?.viewControllers {
    if viewControllers.count > 3 {
        self.navigationController?.popToViewController(viewControllers[viewControllers.count - 3], animated: true)
    } else {
                // fail
    }
}
like image 112
larva Avatar answered Mar 17 '26 10:03

larva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!