Programmatically go back to previous ViewController in Swift

Christian picture Christian · Feb 27, 2015 · Viewed 240.5k times · Source

I send the user over to a page on a button click. This page is a UITableViewController.

Now if the user taps on a cell, I would like to push him back to the previous page.

I thought about something like self.performSegue("back").... but this seems to be a bad idea.

What is the correct way to do it?

Answer

Praveen Gowda I V picture Praveen Gowda I V · Feb 27, 2015

Swift 3:

If you want to go back to the previous view controller

_ = navigationController?.popViewController(animated: true)

If you want to go back to the root view controller

_ = navigationController?.popToRootViewController(animated: true)