The basic basis of Swift dialogs

The basic basis of Swift dialogs

Suppose a non-engineer friend who is studying Swift recently asks, "What is a dialog? How do you build it?" I will summarize the basics of the dialog.

What is a dialog?

Abbreviation for a small window "dialog box" that appears to prompt the user for input or to notify them of something.

For example, after pressing the logout button when logging out, "Are you sure you want to log out?" (Choice: "Yes" or "No") is displayed at the top of the display screen.

Implementation code

First, I will write the basic code.

//This time, create it as a displayDialog method.
func displayDialog(){
  //The main subject is from here
  //=====================
  //STEP1
  //Set an instance of the dialog.
  //Set title and message as String type
  //preferredStyle explained later
  //=====================
  let mDialog = UIAllertControlle(title: "title", message: "Message content", preferredStyle: .alert)
  
  //=====================
  //STEP2
  //Create a choice (button) * Only one this time
  //Select title and style * Style will be described later
  //=====================
  mDialog.addAction(UIAlertAction(title: "Button title", style: .default, handler: { action in
            //Enter the operation when tapping here
        }))

  //=====================
  //STEP3
  //Show dialog
  //=====================
  self.present(mDialog,animated: true,completion: nil)

}

STEP1 Set the dialog instance + enter various values

First, create an instance of the dialog "AlertController". "Title" "message" "preferredStyle" is required as an argument.

title Enter the title of the dialog in String type

message Enter the body to be displayed in the dialog as a String type

preferredStyle .alert: Display in the center of the screen .actionSheet: Display at the bottom of the screen in a format that rises from the bottom.

STEP2 Set dialog buttons

Use in .addAction to set options (buttons) in the dialog. Set the action when selected by UIAlertAction. "Title", "style" and "handler" are required as arguments.

title Enter the button title in String type

style Default: Normal choice Destructive: Displayed in red (* Used when displaying negative choices) Cancel: Displayed at the bottom and only one can be displayed

handler Enter the action when tapping

STEP3 Display dialog

Display using present

bonus

For dialogs, there is a library called SCLAlertView that allows you to easily display cool animated dialogs. You may try using that.

[Swift] About SCLAlertView, which can easily display cool animation alerts

Summary

Putting it all together like this, I found that the dialog is very simple, but there are options such as each option, and you can make various adjustments according to the purpose. There is a big merit in learning systematic knowledge even in the basic part by stopping the output once.

Normally, I use Default without thinking about it, so I feel the need to know the options that can be specified like this time.

I would like to make detailed settings in the future according to the intended use.

Recommended Posts

The basic basis of Swift dialogs
The basic basis of Swift custom cells
The basic basis of Swift's Delegate
Understand the basic mechanism of log4j2.xml
[Swift] Change the textColor of UIDatePicker
Basic basis of Android asynchronous processing "AsyncTask"
[Swift] Vaguely grasp the flow of Delegate
[Swift] Get the height of Safe Area
[Swift] Change the color of SCN Node
[Swift] Termination of the program by the fatalError function
[Swift] How to get the number of elements in an array (super basic)
[Swift] Get the number of steps with CMP edometer
The world of clara-rules (2)
Judgment of the calendar
The world of clara-rules (4)
Image processing: The basic structure of the image read by the program
The world of clara-rules (1)
The world of clara-rules (3)
Basic format of Dockefile
[Swift] This is the solution! Illustration of Delegate implementation
The world of clara-rules (5)
The idea of quicksort
Find the approximate value of log (1 + x) in Swift
[Swift] How to get the document ID of Firebase
The idea of jQuery
Review the basic knowledge of ruby that is often forgotten
Now, I understand the coordinate transformation method of UIView (Swift)
[Swift] Lightly introduce the logic of the app that passed the selection
I tried to summarize the basic grammar of Ruby briefly
[Swift] Get the timing when the value of textField is changed
Aiming for a basic understanding of the flow of recursive processing
Basic methods of Ruby hashes
About the handling of Null
[Swift] Types of types-Basic knowledge-
Basic knowledge of SQL statements
Basic methods of Ruby arrays
[Docker] Introduction of basic Docker Instruction
About the description of Docker-compose.yml
Understand the basics of docker
swift CollectionView Super basic usage
Explanation of the FizzBuzz problem
The basics of Swift's TableView
Super basic usage of Eclipse
Median of the three values
[Ruby] List of basic commands
The illusion of object orientation
Summary of basic functions of ImageJ
Switch the version of bundler
Review of Ruby basic grammar
Defeat the hassle of treating C arrays as Tuples in Swift
[Swift UI] How to get the startup status of the application [iOS]
The point of addiction when performing basic authentication with Java URLConnection
[Swift] The color of the Navigation Bar is different (lighter) from the specified color.
[Swift] Templates and explanations of basic functions using Map Kit View
[Swift] Determine the constellation from the date of birth entered in the UIDatePicker