YogeshChauhan . com

How to show confirmation alerts with OK and cancel buttons using Swift 5?

in Swift on March 9, 2020

We can use UIAlertController class to accomplish that.

It's an object that displays an alert message to the user.

Declaration


class UIAlertController : UIViewController

We will use this class to configure alerts and action sheets with the message that we want to display and the actions from which to choose. After configuring the alert controller with the actions and style we want, we will present it using the present(_:animated:completion:) method. UIKit displays alerts and action sheets modally over app's content.

present(_:animated:completion:)

It's an Instance Method that presents a view controller modally.

Declaration


func present(_ viewControllerToPresent: UIViewController, 
    animated flag: Bool, 
  completion: (() -> Void)? = nil)

Parameters

viewControllerToPresent: The view controller to display over the current view controller’s content.

flag: Pass true to animate the presentation; otherwise, pass false.

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

In addition to displaying a message to a user, we can associate actions with the alert controller to give the user a way to respond. For each action we add using the addAction(_:) method, the alert controller configures a button with the action details. When the user taps that action, the alert controller executes the block you provided when creating the action object.

addAction(_:)

It's an instance Method that attaches an action object to the alert or action sheet.

Declaration


func addAction(_ action: UIAlertAction)

Parameters

action: The action object to display as part of the alert. Actions are displayed as buttons in the alert. The action object provides the button text and the action to be performed when that button is tapped.

The following example only presents OK alert:


let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert) 

alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in 
NSLog("The "OK" alert occured.")

}))

self.present(alert, animated: true, completion: nil)

One more example with OK and Cancel buttons:


let alert = UIAlertController(title: "Add title here", message: "Add your message here.", preferredStyle: UIAlertController.Style.alert)

alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action: UIAlertAction!) in
      
       //add additional code to execute when user clicks on OK 
}))

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in

       //add additional code to execute when user clicks on cancel 
}))

present(alert, animated: true, completion: nil)
amazon

Most Read

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

Recently Posted

May 7 How to disable right click and drag and drop of images using jQuery? May 7 How to render Lists in React? May 7 What’s the difference between variables in CSS and SCSS (Sass)? May 7 How to define variables in SCSS (Sass)? May 7 How to show and hide an element on click in React? May 5 Use inline if to make a shorter conditional syntax in React

You might also like these

How different is Handling Events in React vs HTML DOM?ReactHow to detect if browser supports WebP format on server side PHP script?PHPKanban vs Scrum: The two frameworks of agile principlesMiscDebugging in WordPress Part 3: SCRIPT_DEBUG and SAVEQUERIESWordPressHow to obfuscate JavaScript code to hide it from View Source?JavaScriptHow to get ACF values from custom post type?WordPress