I will summarize it as my memorandum.
This time, I will implement UIAlertController in separate files.
It is very easy to use and can be used in various situations, so please use it if you like!
The file name can be anything, but this time I'll try to make it "Alert.swift" for clarity.
Alert.swift
import UIKit
final class Alert {
//OK only alert
static func okAlert(vc: UIViewController, title: String, message: String, handler: ((UIAlertAction) -> Void)? = nil) {
let okAlertVC = UIAlertController(title: title, message: message, preferredStyle: .alert)
okAlertVC.addAction(UIAlertAction(title: "OK", style: .default, handler: handler))
vc.present(okAlertVC, animated: true, completion: nil)
}
// OK&Cancel alert
 static func cancelAlert(vc: UIViewController, title: String, message: String, handler: ((UIAlertAction) -> Void)? = nil) {
let cancelAlertVC = UIAlertController(title: title, message: message, preferredStyle: .alert)
cancelAlertVC.addAction(UIAlertAction(title: "OK", style: .default, handler: handler))
cancelAlertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
vc.present(cancelAlertVC, animated: true, completion: nil)
}
//Alert with TextField
 static func textFieldAlert(vc: UIViewController, title: String, message: String, placeholder: String, securyText: Bool, handler: ((String?) -> Void)? = nil) {
let textFieldAlertVC = UIAlertController(title: title, message: message, preferredStyle: .alert)
textFieldAlertVC.addTextField { (textField) in
textField.placeholder = placeholder
textField.isSecureTextEntry = securyText
}
textFieldAlertVC.addAction(UIAlertAction(title: "OK", style: .default, handler: { (_) in
handler?(textFieldAlertVC.textFields?.first?.text)
}))
textFieldAlertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
vc.present(textFieldAlertVC, animated: true, completion: nil)
}
//Alerts that disappear automatically
static func autoCloseAlert(vc: UIViewController, title: String, message: String) {
let autoCloseAlertVC = UIAlertController(title: title, message: message, preferredStyle: .alert)
vc.present(autoCloseAlertVC, animated: true) {
// 1.Delete after 5 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
autoCloseAlertVC.dismiss(animated: true, completion: nil)
}
}
}
The caller of the above implementation code looks like this.
ViewController.swift
//Show alert for OK button only
Alert.okAlert(vc: self, title: "OK alert", message: "Only the OK button!", handler: { (_) in
//Describe the process when you want to do something after pressing the OK button
print("After pressing the OK button!")
})
//Show alerts with OK and Cancel buttons
Alert.cancelAlert(vc: self, title: "OK&Cancel alert", message: "There is also a cancel button!", handler: { (_) in
//Describe the process when you want to do something after pressing the OK button
print("After pressing the OK button!")
})
//OK with TextField&Show alerts with cancellations
Alert.textFieldAlert(vc: self, title: "TextField alert", message: "You can enter characters!", placeholder: "input", securyText: false, handler: { (text) in
//Describe the processing when using the entered characters
print("I input it\(text)It is displayed!")
})
//Show alerts that disappear automatically
Alert.autoCloseAlert(vc: self, title: "Alerts that disappear automatically", message: "It disappears automatically!")
How was it?
This is my first time writing Qiita, so I think there are many points that cannot be reached. After that, it's easier to use than the code above! If you have any implementation code, please comment! : open_hands:
Recommended Posts