title: Programmatically press back button for UIViewController with UITableView iOS swift
body: <p>I have a UIViewController that implements UITableViewDelegate, UITableViewDataSource and that contains a UITableView as a member variable. When a user click on one of the rows of that table, the app performs a storyboard segue to open the detail view controller. That detail view controller of course has a button in the top left of the screen that is the "back" button to go back up to the UIViewController with the UIViewTable.</p>
  
  <p>So, suppose that I want to programmatically "click" that back button. How exactly would I do that in swift? This is the most recent version of swift (swift 4?) in XCode 10.1.</p>
  
  <p>UPDATE:</p>
  
  <p>So here is how I solved this. As the answers below show, it is possible to use self.navigationController?.popViewController(animated: true) to just return to the previous view controller. What I discovered I also wanted to do, however, was to call a specific method in that view controller so that it executed a certain behavior once it got shown. It turns out that is also possible, but in my case it was a bit tricky, since that prior view controller was actually a UITabBarController. Therefore I had to get the ViewController that I was interested in from the UITabBarController. I did it like this:</p>
  
  <pre><code>let numvc = navigationController!.viewControllers.count
  let tvc:UITabBarController = navigationController!.viewControllers[numvc-2] as! UITabBarController
  let my_vc: MyCustomVC = tvc.viewControllers![0] as! MyCustomVC
  my_vc.some_function()
  </code></pre>
  
  <p>Here of course MyCustomV is my custom view controller class and some_function() is the method I want to call on that class. Hope this helps someone.</p>