Search code examples
iosswiftswift5

Ways of changing view in Xcode


I'm new in Swift and I have a question: What are the best ways to change view in xcode? What's the difference between using segue and instantiateViewController?


Solution

  • Apple explains it best in their documentation:

    "You can initiate the presentation of a view controller programmatically or using segues. If you know your app’s navigation at design time, segues are the easiest way to initiate presentations. For more dynamic interfaces, or in cases where there is no dedicated control to initiate the segue, use the methods of UIViewController to present your view controllers."

    Source: https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/PresentingaViewController.html#//apple_ref/doc/uid/TP40007457-CH14-SW1

    I like to use segues for connecting view controllers in my storyboard, and for easily passing data to the presented VC. I programmatically present view controllers when I want to customize the animation and/or size of the presented controller. Another reason to programmatically initialize a VC is when you just want its view added as a sub-view of your current VC. You would likely do this with an AVPlayerViewController for example.