-1

I’m making an application on UITableView and I want to make a function, when I click on a cell, I open another view controller, on which there will be a back button, I want that when I click on this button, the newly opened controller closes, and returns me to that the controller from which it was opened, how to do it?

This code is for creating a new controller when clicking on a cell (without the function to open the controller, because I don’t know yet with which method I can implement this)

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = createVC(rootVC: AboutRecipeViewController(), TBImageName: nil, TBTitle: recipes[indexPath.row].recipeName)
}
Vladislav
  • 1
  • 3
  • 1
    Does this answer your question? [Table View Controller each row connected to different view controller](https://stackoverflow.com/questions/27775630/table-view-controller-each-row-connected-to-different-view-controller) – timbre timbre Jun 09 '20 at 19:04

1 Answers1

0

The new ViewController got a method for that. dismiss(animated: true, completion: nil)

Create an IBAction for your Back Button and run this method when the button got pressed. That should dismiss the new ViewController and get you back to the initial one.

However, your syntax with the back button seems to be similar to a UINavigationViewController. Consider using this makes it way more easy.

christophriepe
  • 1,157
  • 12
  • 47
  • "However, your syntax with the back button seems to be similar to a UINavigationViewController. Consider using this makes it way more easy." what do you mean? – Vladislav Jun 10 '20 at 19:49
  • @Vladislav UIKit has an UIComponent for something, that sounds pretty similar to the goal you wanna achieve. An UINavigationController lets you easily navigate between Views. Every time, you see this back button in the top left corner, a UINavigationViewController was used. Check this out: https://developer.apple.com/documentation/uikit/uinavigationcontroller – christophriepe Jun 11 '20 at 20:44