Instantiate and Present ViewController in Swift: Complete Guide with Answers to Common Questions

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Instantiate and Present ViewController in Swift: Full Breakdown with Examples and Answers to Common Questions

\n\n

Hello, 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\n

This 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\n

1. Basic Method: Instantiate from Storyboard

\n\n

The 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\n

Step 1: Setting Up Storyboard ID

\n

Open 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.

\n\n

Step 2: Code for instantiate and present

\n

Now, in the appropriate place in your code (e.g., on a button tap), write:

\n\n
import 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\n

This 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.

\n\n

2. Present vs Push: When to Use What?

\n\n

Many 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
\n\n

For push, use the method navigationController?.pushViewController(_:animated:).

\n\n
// 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\n

3. Passing Data Before Present

\n\n

One 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\n

Example with Passing a String

\n

Suppose DetailViewController has a property:

\n\n
class DetailViewController: UIViewController {\n    var receivedMessage: String = ""\n    \n    ov                

Blogs

Book Recommendations