My code works, but I'm trying to understand it. It's a bit of code used to manage a "Cancel" button that can return from either a "Show" segue (presented when user clicks on a table view cell for editing) or a "Present Modally" segue (when the user clicks a "+" bar button item to add a new cell to the table view). Diagram below. I'm getting confused by UINavigationController and the navigationController property. Apologies if I'm missing something very obvious.
// Apple says below is nil if neither the current view controller nor any of its ancestors were presented modally
let isPresentingInAddMode = presentingViewController is UINavigationController
if isPresentingInAddMode {
// Modal segues need to be dismissed
dismiss(animated: true, completion: nil)
} else {
// But Show segues are "popped" off of a stack of controllers.
navigationController!.popViewController(animated: true)
}
Here's what I am not understanding: - If I've arrived at the Detail View Controller via a "Show" segue, pause during execution, and option-click on navigationController, Xcode says that it's a UINavigationController? And if I pause execution inside the else condition & use the debugger to: po navigationController! == nil - Xcode says it this evaluates to false, so navigationController is a valid UINavigationController.
So why doesn't
presentingViewController is UINavigationController
equate to true in the topmost statement when I've presented using a "Show"?
Perhaps I'm not understanding "present". Is presenting something that only happens with modal segues, so there's no presentingViewController?
And if I look back at my storyboard (see diagram below) one of the ancestors for the Detail View Controller is a table view controller that has a navigation controller embedded in it given Apple's definition of presentingViewController, shouldn't:
presentingViewController is UINavigationController
Be true in this situation, too?
And would:
presentingViewController != nil
achieve the same result or is there an important reason to verify the presentingViewController is a UINavigationController?
Thanks much for anyone kind enough to help me parse this out. John
The "ShowDetail" segue is a "Show" segue originating from the table view cell. The "AddItem" segue is of kind "Present Modally" and is originating from the "+" add bar button item.
It's likely not necessary to see the prepare for segue code in the table view controller, but if you're curious:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "ShowDetail" { // is this the "ShowDetail" segue? and if it is...
// ... get the IndexPath for the row the user clicked on (the selected row)
let indexPath = tableView.indexPathForSelectedRow!
let destinationViewController = segue.destination as! DetailViewController // downcast the destination as the specific class DetailViewController
// Get the to do item that the user clicked on
let selectedToDo = toDoArray[indexPath.row]
// Pass selectedToDo to the toDoItem variable in our destinationViewController
destinationViewController.toDoItem = selectedToDo
}
}