title: Swift difficulty understanding UINavigationController & navigationController re: presentingViewController
body: <p>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.</p>
  
  <pre><code>// 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)
  }
  </code></pre>
  
  <p>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 &amp; use the debugger to:
  po navigationController! == nil
  -   Xcode says it this evaluates to false, so navigationController is a valid UINavigationController. </p>
  
  <p>So why doesn't </p>
  
  <pre><code>presentingViewController is UINavigationController 
  </code></pre>
  
  <p>equate to true in the topmost statement when I've presented using a "Show"?<br>
  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?</p>
  
  <p>And would:</p>
  
  <pre><code>presentingViewController != nil
  </code></pre>
  
  <p>achieve the same result or is there an important reason to verify the presentingViewController is a UINavigationController?</p>
  
  <p>Thanks much for anyone kind enough to help me parse this out.
  John</p>
  
  <p><a href="https://i.stack.imgur.com/qX3Br.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qX3Br.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/9CZcS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9CZcS.png" alt="enter image description here"></a></p>
  
  <p>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.</p>
  
  <p>It's likely not necessary to see the prepare for segue code in the table view controller, but if you're curious:</p>
  
  <pre><code>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
      }
  }
  </code></pre>