Swift
iOS Development
ViewController
Programming Tutorial
Code Example

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:

swift
1// Called within the presented view controller
2func someAction() {
3    self.dismiss(animated: true, completion: nil)
4}

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:

  1. Define a protocol:
swift
   protocol DismissDelegate: AnyObject {
       func dismissViewController()
   }
  1. Set the delegate:
swift
1   class ChildViewController: UIViewController {
2       weak var delegate: DismissDelegate?
3
4       @IBAction func dismissTapped(_ sender: UIButton) {
5           delegate?.dismissViewController()
6       }
7   }
  1. Implement the delegate in the presenting view controller:
swift
1   class ParentViewController: UIViewController, DismissDelegate {
2       func presentChildViewController() {
3           let childVC = ChildViewController()
4           childVC.delegate = self
5           present(childVC, animated: true, completion: nil)
6       }
7
8       func dismissViewController() {
9           dismiss(animated: true, completion: nil)
10       }
11   }

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:

swift
// Called within a view controller part of UINavigationController
self.navigationController?.popViewController(animated: true)

Alternatively, to dismiss the entire navigation stack:

swift
self.navigationController?.dismiss(animated: true, completion: nil)

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

MethodUse CaseExample Code Snippet
dismiss(animated:completion:)Basic dismissal of presented controllerself.dismiss(animated: true, completion: nil)
Delegate PatternWhen dismissing deeply nested controllersdelegate?.dismissViewController()
popViewController(animated:)For controllers in a UINavigationControllerself.navigationController?.popViewController(animated: true)
Storyboard Exit SegueUsing storyboard interface builderSet 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.


Course illustration
Course illustration

All Rights Reserved.