Search code examples
iosswiftbuttonstoryboard

Swift: Back Button shows in Storyboard but not Simulator


Back Button shows in StoryBoard but not Simulator.

I added a segue from TableViewController to DetailViewController to pass data. The Storyboard automatically shows a Back Button on DetailViewController, but when I run the Simulator, the button doesn't show up.

This is my Storyboard:

enter image description here

A closer look of TableViewController and DetailViewController: enter image description here

But in my Simulator the button doesn't show up:

enter image description here

The hierarchy of the whole project:

enter image description here

I want to know where to configure the back button(in my segue?), or instead deleting the button(not letting it show in the Storyboard) so I can create one myself.

The code of my segue in my TableViewController:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showDetailView", sender: indexPath)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch (segue.destination, sender) {
    case (let controller as DetailViewController, let indexPath as NSIndexPath):
        controller.receivedName = storeList[indexPath.row].name!
        controller.receivedDesc = storeList[indexPath.row].desc!
        controller.receivedRate = storeList[indexPath.row].rate
        controller.receivedUrl = storeList[indexPath.row].url!

    default:
        print("unknown segue")
        break
    }
}

Solution

  • Your initial view controller in the storyboard is actually the TabelViewController (you can see there is an arrow to it).

    Thats' why when you start the scene and the TableViewController shows but it is not embedded in the navigation because the navigation controller has never been created.

    Just change which is the initial view controller to be the navigation controller or any other before the navigation controller which holds the TableViewController.

    You can just drag the arrow to change the initial controller in the storyboard