LoginSignup
2
2

More than 3 years have passed since last update.

UIAlertControllerをファイルを分けて実装してみる

Last updated at Posted at 2020-12-20

自分の備忘録としてまとめておきます。

はじめに

今回は、UIAlertControllerをファイルを分けて実装してみようと思います。

非常に使いやすく、色んな場面で使えるので皆さんも良かったら使ってみて下さい!

実装

ファイル名はなんでも良いですが、今回は分かりやすく「Alert.swift」にしようと思います。

Alert.swift
import UIKit

final class Alert {
 // OKのみアラート
 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&キャンセルアラート
 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: "キャンセル", style: .cancel, handler: nil))
   vc.present(cancelAlertVC, animated: true, completion: nil)
 }

 // 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: "キャンセル", style: .cancel, handler: nil))
   vc.present(textFieldAlertVC, animated: true, completion: nil)
 }

// 自動で消えるアラート
 static func autoCloseAlert(vc: UIViewController, title: String, message: String) {
  let autoCloseAlertVC = UIAlertController(title: title, message: message, preferredStyle: .alert)
  vc.present(autoCloseAlertVC, animated: true) {
   // 1.5秒後に削除
   DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
     autoCloseAlertVC.dismiss(animated: true, completion: nil)
     }
   }
 }

使い方

上の実装コードを、呼び出す側ではこうなります。

ViewController.swift
   // OKボタンのみのアラートを表示
   Alert.okAlert(vc: self, title: "OKアラート", message: "OKボタンのみです!", handler: { (_) in
     // OKボタンを押した後に何かしたい場合の処理を記述
     print("OKボタンを押した後ですよ!")
   })

   // OKボタンとキャンセルボタンがあるアラートを表示
   Alert.cancelAlert(vc: self, title: "OK&キャンセルアラート", message: "キャンセルボタンもあります!", handler: { (_) in
     // OKボタンを押した後に何かしたい場合の処理を記述
     print("OKボタンを押した後ですよ!")
   })

   // TextField付きOK&キャンセルがあるアラートを表示
   Alert.textFieldAlert(vc: self, title: "TextFieldアラート", message: "文字を入力できます!", placeholder: "入力", securyText: false, handler: { (text) in
     // 入力した文字を使う場合の処理を記述
     print("入力した\(text)を表示します!")
   })

  // 自動で消えるアラートを表示
  Alert.autoCloseAlert(vc: self, title: "自動で消えるアラート", message: "自動で消えます!")

まとめ

如何だったでしょうか?

Qiitaを書くの今回が初めてなので、至らぬ点が多いと思います。
後、上のコードよりも、もっと使いやすいよー!っていう実装コードがあったらコメントして下さると嬉しいです!:open_hands:

2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2