Instantiate and Present ViewController in Swift: Full Breakdown with Examples and Answers to Common Questions
\n\nHello, colleagues! If you've ever faced the task of programmatically creating and displaying a new screen in an iOS app using Swift, you've probably wondered: "How do I properly create a ViewController from Storyboard?", "How do I pass data before showing it?" and "What's the difference between Segue and manual Present?".
\n\nThis topic is one of the most popular on Stack Overflow (thousands of views!), and for good reason. At first glance, everything seems simple, but the devil, as always, is in the details. Let's break down all the nuances so you never get confused with instantiate viewController Swift and present viewController Swift again.
\n\n1. Basic Method: Instantiate from Storyboard
\n\nThe most common scenario is having a Storyboard with a controller that has a Storyboard ID assigned. Without this identifier, you won't be able to create an instance of the controller programmatically.
\n\nStep 1: Setting Up Storyboard ID
\nOpen the Storyboard, select the desired ViewController, go to the Identity Inspector (the ID card icon), and in the Storyboard ID field, enter a unique name, for example DetailViewController. Also, make sure the Use Storyboard ID checkbox is active.
Step 2: Code for instantiate and present
\nNow, in the appropriate place in your code (e.g., on a button tap), write:
\n\nimport UIKit\n\nclass MainViewController: UIViewController {\n\n @IBAction func showDetailTapped(_ sender: UIButton) {\n // 1. Get a reference to the Storyboard\n let storyboard = UIStoryboard(name: "Main", bundle: nil)\n \n // 2. Create an instance of the controller by its Storyboard ID\n guard let detailVC = storyboard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController else {\n fatalError("Could not find controller with ID DetailViewController")\n }\n \n // 3. Present the controller modally\n present(detailVC, animated: true, completion: nil)\n }\n}\n\nThis is a classic example of instantiate viewController Swift. Note the guard let — this is safe unwrapping that protects your app from crashing if the ID was entered incorrectly.
2. Present vs Push: When to Use What?
\n\nMany beginners confuse present (modal presentation) and push (pushing onto the navigation stack). Let's break down the difference.
\n\n- \n
- Present — this is modal presentation. The controller appears from the bottom (or with another animation) and is not tied to the navigation stack. Used for temporary screens: login, alerts, forms. \n
- Push — works only inside a UINavigationController. The controller is "pushed" onto the stack, appears from the right (by default), and automatically gets a "Back" button. Ideal for hierarchical screens: list -> details. \n
For push, use the method navigationController?.pushViewController(_:animated:).
// Push (requires UINavigationController)\nif let navigationController = navigationController {\n navigationController.pushViewController(detailVC, animated: true)\n} else {\n present(detailVC, animated: true, completion: nil) // fallback\n}\n\n3. Passing Data Before Present
\n\nOne of the most common questions: "How do I pass data to a new ViewController before it appears?" The answer is simple: use the controller's properties or methods before calling present.
\n\nExample with Passing a String
\nSuppose DetailViewController has a property:
\n\nclass DetailViewController: UIViewController {\n var receivedMessage: String = ""\n \n ov