Try to implement UIAlertController by separating files

I will summarize it as my memorandum.

Introduction

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!

Implementation

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)
     }
   }
 }

How to use

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!")

Summary

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

Try to implement UIAlertController by separating files
Try to implement iOS14 Widget function
[Java] Try to implement using generics
Try to implement Yubaba in Java
Try to implement n-ary addition in Java
Try to implement login function with Spring-Boot
Try to sort classes by enumeration type
Try to implement login function with Spring Boot
Try to implement TCP / IP + NIO with JAVA
I want to delete files managed by Git
Try to implement using Rakuten product search API (easy)
Try to implement tagging function using rails and js
Try to release gem
I tried to implement the like function by asynchronous communication