0
votes

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.

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.

UPDATE:

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:

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

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.

2

2 Answers

2
votes

When You run a segue you perform a "pushViewController" method to the next view, so if you want to go back to the previous view programmatically you just have to do is pop the last view like so:

self.navigationController?.popViewController(animated: true)

UPDATE You just need the if statement if you have multiple segues from that viewController, if not, you can delete and just cast the next view as you wish and set the properties, let the autocomplete write the *prepare(for segue... * method for you, so You don't run into any problems

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "yourSegueName" {
        let destinationVC = segue.destination as! CustomViewController
        destinationVC.labelExample.text = "Some text I'm sending"
    }
}
1
votes

Are you sure you need to "click" the button?

  • If all you need is to dismiss details view controller, you can just call navigationController?.popViewController(animated: true)
  • Or if you want to deal directly with button, you can tell it to send its actions: backButton.sendActions(for: .touchUpInside)
  • Or if you absolutely need to show button clicking animation, then you will need something like this (you should play and choose suitable delay):
    backButton.isHighlighted = true
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
        backButton.isHighlighted = false
        backButton.sendActions(for: .touchUpInside)
    }