How to dismiss ViewController in Swift?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Overview
In iOS development using Swift, displaying and dismissing view controllers are crucial processes that form the backbone of navigation in an app. This article will provide a comprehensive guide on how to dismiss a UIViewController effectively, along with some practical examples, essential considerations, and best practices.
Understanding View Controllers
A UIViewController manages a view hierarchy in iOS applications. It handles the user interface and related interactions, including transitioning between views. In a typical scenario, a view controller is often presented modally or using a navigation controller stack. There are several ways to dismiss a view controller—manually using code or automatically using the storyboard's presets.
Dismissing a ViewController
Basic Dismissal
To dismiss a presented view controller, you can call the dismiss(animated:completion:) method. This method takes two parameters:
animated: A Boolean value that determines whether the dismissal is animated.completion: A closure executed after the view controller is dismissed.
Example:
Dismissing from Nested View Controllers
If you're attempting to dismiss a view controller that was presented from a deeply nested view controller, you often need to communicate through delegate protocols or callbacks to ensure the dismissal command reaches the top-level presented view controller.
Example using Delegate:
- Define a protocol:
- Set the delegate:
- Implement the delegate in the presenting view controller:
Dismissing within a Navigation Controller
If the view controller is embedded inside a UINavigationController, you can simply pop the view controller from the navigation stack.
Example:
Alternatively, to dismiss the entire navigation stack:
Dismissal with Storyboards
If you use storyboards, you can set up an "Exit" segue by dragging from a control (like a button) to the "Exit" icon in the scene dock. Then, implement an @IBAction with a suitable signature in your source view controller corresponding to the exit action.
Key Considerations
When to Dismiss
- User Interaction: Often prompted by user interaction, such as tapping a cancel button or completing a form.
- Data Transfer: Ensure any necessary data is passed back before dismissal.
Animation Choices
- Use animation for a smoother user experience; however, avoid excessive animations that might confuse or annoy users.
Error Handling
Include error handling to manage potential issues during dismissal, such as failures to complete required operations before the view is dismissed.
Summary Table
| Method | Use Case | Example Code Snippet |
dismiss(animated:completion:) | Basic dismissal of presented controller | self.dismiss(animated: true, completion: nil) |
| Delegate Pattern | When dismissing deeply nested controllers | delegate?.dismissViewController() |
popViewController(animated:) | For controllers in a UINavigationController | self.navigationController?.popViewController(animated: true) |
| Storyboard Exit Segue | Using storyboard interface builder | Set up exit IBAction with segue |
Additional Resources
- Apple Documentation: For more intricate details and official guidelines, refer to the Apple Developer Documentation.
- ViewController Lifecycle: Understanding the view controller lifecycle (e.g.,
viewDidLoad,viewWillAppear) helps optimize when and how to dismiss a view controller.
By combining these techniques, developers can dismiss view controllers in a manner that maintains app fluidity and enhances user experience.

