Complete Guide to Instantiating and Presenting UIViewController in Swift

Nov 20, 2025 · Programming · 27 views · 7.8

Keywords: Swift Programming | UIViewController | UIStoryboard

Abstract: This article provides a comprehensive exploration of the complete process for instantiating and presenting UIViewController from UIStoryboard in the Swift programming language. By comparing syntax differences between Objective-C and Swift, it deeply analyzes core methods for UIStoryboard initialization, view controller instantiation, and modal presentation. The article combines common error scenarios to offer practical solutions for avoiding repeated presentation of the same controller instance, including complete code examples and best practice recommendations.

Analysis of Syntax Differences Between Swift and Objective-C

During the migration from Objective-C to Swift in iOS development, many developers face challenges with syntax conversion. While the core functionality of the UIKit framework remains stable, the API invocation methods have undergone significant changes. The square bracket syntax [class method] in Objective-C has been replaced by the more concise dot syntax class.method() in Swift. This change not only enhances code readability but also better integrates with Swift's language characteristics.

UIStoryboard Initialization Process

Initializing the storyboard object using UIStoryboard is the first step in instantiating a view controller. In Swift, the storyboard instance is created by calling the UIStoryboard(name:bundle:) initializer method. The name parameter corresponds to the storyboard file name (excluding the .storyboard extension), while the bundle parameter is typically set to nil to use the main bundle. This design maintains functional consistency with the Objective-C version while providing a more type-safe interface.

View Controller Instantiation Mechanism

Instantiating a specific view controller from the storyboard requires calling the instantiateViewController(withIdentifier:) method. The key point is that the Storyboard ID must be correctly set in Interface Builder. This identifier is passed as a string parameter to the instantiation method, and the system searches for the corresponding view controller definition in the storyboard file based on this identifier. The returned view controller instance has already completed the connection of all interface elements defined in the storyboard and property initialization.

Implementation of Modal Presentation

Presenting the view controller uses the present(_:animated:completion:) method, which is a direct translation of Objective-C's presentViewController:animated:completion:. The animated parameter controls whether to use the system's default transition animation, while the completion parameter is an optional closure that executes after the presentation operation completes. In most cases, the completion parameter can be omitted, and the system will use the default completion handling logic.

Complete Code Implementation Example

The following code demonstrates the complete process from storyboard instantiation to view controller presentation:

let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "DetailViewController")
self.present(viewController, animated: true)

This example clearly shows the conciseness of Swift syntax compared to Objective-C while maintaining the same functional implementation.

Common Errors and Solutions

An important issue mentioned in the reference article is the runtime error caused by repeatedly presenting the same view controller instance. When attempting to present a controller that is already in the view hierarchy, the system throws an "Application tried to present modally an active controller" exception. The solution is to ensure that each presentation uses a newly instantiated view controller object rather than reusing an existing instance. This emphasizes that when the same interface needs to be presented multiple times, a new controller instance must be re-instantiated through the storyboard.

Best Practice Recommendations

In actual development, it is recommended to define storyboard names and view controller identifiers as constants to avoid maintenance issues caused by hard-coded strings. Additionally, for scenarios requiring data passing, relevant properties of the view controller should be set after instantiation but before presentation. This pattern ensures correct timing for data transfer and avoids data display issues due to improper timing.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.