Enix Enix - 1 month ago 20
Swift Question

What is the difference between UINavigationController.presentViewController and self.presentViewController?

Suppose I have a navigation controller with root view controller A, and I need to present a view controller B, I can do with either of the following two ways:


  1. self.navigationController!.presentViewController(b, animated: true)



or


  1. self.presentViewController(b, animated: true)



But, I just wonder, what is the difference between these two ways?

Answer

They are analogous - both initiate a modal presentation.

In recent iOS versions, modal presentation always travels to the top most container view controller. So when your view controller (aka self) is container inside a navigation controller (aka self.navigationController), when you attempt presenting on the view controller, it will pass the presentation duties to the navigation controller. You can verify this by logging the presentingController of b once the presentation is completed. In both cases, presentingController will be the navigation controller.