[Behavior confirmed in December 2020] How to implement the alert display function

What is the alert function?

image.png It is the one that appears when you tap the logout button.

Implementation code introduction

//Create alert
let alert = UIAlertController(title: "Do you want to log out?", message: "When you log out, the notification settings will be reset.", preferredStyle: .alert)
        
//Alert button
alert.addAction(UIAlertAction(title: "Cancel", style: .default))
alert.addAction(UIAlertAction(title: "Logout", style: .default, handler: { action in }))
//Display alerts
present(alert, animated: true, completion: nil)

First, create an instance with UIAlertController by specifying title, mesaage, preferredStyle.

Next, create a button to operate the alert.

You can create a button with alert.addAction (UIAlertAction (title:" ", style :, handler :)). This is the default and you can omit handler if you don't want to tap the alert button and do anything. ** (If you delete the handler, it means cancel the operation.) **

Next, if you want to do something when you tap the alert button, prepare a closure {action in} in the handler and describe the process in the closure, and the contents will be executed.

Finally, display an alert with present (alert, animated: true, completion: nil) and you're done!

Remarks

If you want to change the color of the cancel button to red, specify style: .destructive of UIAlertAction.

Recommended Posts

[Behavior confirmed in December 2020] How to implement the alert display function
[Swift] How to implement the countdown function
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
[For beginners] How to implement the delete function
[Swift] How to implement the fade-in / out function
How to set the display time to Japan time in Rails
[Rails] How to display an image in the view
How to implement one-line display of TextView in Android development
[Rails] How to display information stored in the database in view
How to add the delete function
[Swift] How to implement the Twitter login function using Firebase UI ①
[Swift] How to implement the Twitter login function using Firebase UI ②
How to implement search functionality in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
[Java] How to use the hasNext function
How to implement coding conventions in Java
How to implement TextInputLayout with validation function
How to get the date in java
How to implement ranking functionality in Rails
[Processing × Java] How to use the function
How to implement asynchronous processing in Outsystems
How to display error messages in Japanese
How to display the text entered in text_area in Rails with line breaks
How to implement the email authentication function at the time of user registration
How to check the logs in the Docker container
How to display a web page in Java
How to add sound in the app (swift)
[Android] Implement a function to display passwords quickly
How to implement a like feature in Rails
How to display the result of form input
How to implement optimistic locking in REST API
How Docker works ~ Implement the container in 60 lines
How to make a follow function in Rails
How to build the simplest blockchain in Ruby
How to check Rails commands in the terminal
How to implement Pagination in GraphQL (for ruby)
How to implement image posting function using Active Storage in Ruby on Rails
How to implement UICollectionView in Swift with code only
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
[Rails, JS] How to implement asynchronous display of comments
Organized how to interact with the JDK in stages
How to display a browser preview in VS Code
How to specify the resource path in HTML import
[Swift] How to display the entered characters in Widget via UserDefaults when using WidgetKit
How to debug the generated jar file in Eclipse
Summary of how to implement default arguments in Java
Rails learning How to implement search function using ActiveModel
I tried to implement the Euclidean algorithm in Java
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap
How to get the class name / method name running in Java
How to correctly check the local HTML file in the browser
How to use the getter / setter method (in object orientation)
How to display the select field of time_select every 30 minutes
How to set chrony when the time shifts in CentOS7
[Swift] I tried to implement the function of the vending machine
How to retrieve the hash value in an array in Ruby
How to implement infinite scrolling (page nate) in Swift TableView