Can't Dismiss View Controller That's Embedded in a Navigation Controller

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

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!

Dismiss ViewController opened from SideMenu

After searching for a while I still could not locate source of my issue so I stayed with workaround:

Close button just opens previous UIViewController but this is not a good practice.

In my case application has only 3 screens and it's fine, but if to talk about bigger apps such solution is not recommended.

Stop ViewController outside of navigation controller

For anyone confused by this like I was, here is what I ended up doing.

As Ankit mentioned, a pop segue was the way to go, yet you can't POP a UIViewController off of a navigation stack that isn't there. So to fix that, I had to embed my Main View controller into a UINavigationController, then push both the game and game over vc's onto the navigation stack. Once the game was over, I did an unwind segue back to the Main vc.

Main Menu -> (Modal) New Game -> (PUSH) Game -> (PUSH)Game over -> (Unwind) Main

There is an option in interface builder to hide the navigation bar on a UINavigationController which is what I did.

Bottom Line: this flow I was using presupposed a navigation stack and thus required a UINavigationController. After realizing this things made more sense.

Dismiss a SwiftUI View that is contained in a UIHostingController

UPDATE: From the release notes of iOS 15 beta 1:

isPresented, PresentationMode, and the new DismissAction action dismiss a hosting controller presented from UIKit. (52556186)


I ended up finding a much simpler solution than what was offered:


final class SettingsViewController: UIHostingController<SettingsView> {
required init?(coder: NSCoder) {
super.init(coder: coder, rootView: SettingsView())
rootView.dismiss = dismiss
}

func dismiss() {
dismiss(animated: true, completion: nil)
}
}

struct SettingsView: View {
var dismiss: (() -> Void)?

var body: some View {
NavigationView {
Form {
Section {
Button("Dimiss", action: dismiss!)
}
}
.navigationBarTitle("Settings")
}
}
}

How to dismiss ViewController in Swift?

From you image it seems like you presented the ViewController using push

The dismissViewControllerAnimated is used to close ViewControllers that presented using modal

Swift 2

navigationController.popViewControllerAnimated(true)

Swift 4

navigationController?.popViewController(animated: true)

dismiss(animated: true, completion: nil)


Related Topics



Leave a reply



Submit