Search code examples
swiftuiviewcontrolleruinavigationcontroller

iOS Swift: Dismiss View Controller That Was Presented Using "Show Detail"


I have a view controller that is embedded in a navigation controller. I use show detail in storyboard to show it. How would I dismiss this view controller or go back to previous one?

I know back functionality comes automatically when using "Show Detail" but I need to embed this one in a navigation controller, which doesn't allow the back functionality, so I needed to put a "Cancel" bar button, but It's not working. I tried these ways but didn't work:

    dismiss(animated: true, completion: nil)
navigationController?.popViewController(animated: true)
self.presentingViewController?.dismiss(animated: true, completion: nil)

Solution

  • You can add an unwind segue from the detail controller to the master controller.

    In your master controller, add a method that handles the unwind action:

    @IBAction func unwindFromDetail(segue: UIStoryboardSegue) {
    
    }
    

    Then in the storyboard, control drag from detail controller to the "Exit" of master controller, then select the above method.

    Give the segue an identifier and perform it!