I have two view controllers. I have navigated from one view to another view by press the button to using below code.
*let secondViewController = self.storyb
Swift 5(Working tested). Better way to check whether the view controller is stacked in navigation or not is by simply asking for the view controller's accessibility.See example below.I use this snippet to go back to previous view controller.
if self.navigationController?.accessibilityActivate() != nil {
self.navigationController?.popViewController(animated: true)
} else {
self.dismiss(animated: true, completion: nil)
}