Yogesh Chauhan's Blog

How to add Go Back button in Swift 5?

in Swift on April 28, 2020

Step 1. Add a button in the view controller first.

Step 2. Add this code in button IBAction function.


    self.dismiss(animated: true, completion: nil)

So your function would look like this:


    @IBAction func goBackButton(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }

Detailed Discussion

dismiss(animated:completion:)

Dismisses the view controller that was presented modally by the view controller.

Declaration


func dismiss(animated flag: Bool, 
  completion: (() -> Void)? = nil)

Parameters

flag: Pass true to animate the transition.

completion: The block to execute after the view controller is dismissed. This block has no return value and takes no parameters. You may specify nil for this parameter.

Discussion

The presenting view controller is responsible for dismissing the view controller it presented.

If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack.

When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack.

The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

If you want to retain a reference to the view controller's presented view controller, get the value in the presentedViewController property before calling this method.

The completion handler is called after the viewDidDisappear(_:) method is called on the presented view controller.

Credit: Apple Dev


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS? #Jan 8 How to Install Xcode Command Line Tools on MacOS?
You might also like these
Advanced Array Methods in JavaScript (with examples)JavaScriptWhat is XP (Extreme Programming) and When should it be used?MiscellaneousHow to Draw a Text Image using JavaScript?JavaScriptHow to draw an SVG path on scroll using JavaScript?JavaScriptHow to set opacity or transparency using CSS?CSSWordPress: How to get ACF field values from another post?WordPress