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.
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.
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)
}
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.
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
Display using present
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
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