iosuikitinterface-builderxcode-storyboard

SplitViewController: How do I replace a the secondary view rather than pushing a new one?


I have a simple storyboard as shown below. Clockwise from the split view controller we have the navigation controller, a "theme chooser" and a little game. All of the buttons in the theme chooser view segue to the game, telling it what colours to use etc. All of these segues are of type "Show Detail (e.g. replace)", not "Show (e.g. push)". I want the buttons to replace the game view with a new game view, but instead it creates the new game view on top of the old one, so if you press back on the new one, it goes back to the old one. I thought the point of "show detail" was to replace rather than add. What am I misunderstanding there and how do I get it to replace?

enter image description here

Cheers!


Solution

  • I figured it out!

    Since some iOS update, you need to embed your detail view in a navigation view; i.e.,

    storyboard

    So, I changed my segue preparing code from:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "Choose Theme" {
            if let themeName = (sender as? UIButton)?.currentTitle, let theme = themes[themeName] {
                if let cvc = segue.destination as? ConcentrationViewController {
                 cvc.theme = theme
                }
            }
        }
    }
    

    to

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "Choose Theme" {
            if let themeName = (sender as? UIButton)?.currentTitle, let theme = themes[themeName] {
                if let navigationController = segue.destination as? UINavigationController,
                   let cvc = navigationController.topViewController as? ConcentrationViewController {
                    cvc.theme = theme
                }
            }
        }
    }
    

    And now it works.