LoginSignup
0
0

More than 1 year has passed since last update.

【Swift】UIAlertViewControllerの実装

Posted at

はじめに

ユーザーに確認を求めたりエラー表示をしたりとよくアラートを出したい場面があると思います。
そんな時に毎回アラートを出すコードを書くのは効率が悪いので、どこでもすぐ呼び出せるようにアラート処理用のファイルを作成しておきます。
コピーして使用できるので特に初学者の方のお役に立てればと思います。

アラートを実装

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) {
            // 2秒後に消える
            DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                autoCloseAlertVC.dismiss(animated: true, completion: nil)
            }
        }
    }
}

呼び出しコード

   // OKアラートを表示
   Alert.okAlert(vc: self, title: "タイトル", message: "任意のメッセージ", handler: { (_) in
     // OKボタンを押した後の処理を書いて下さい。
   })

   // OK&キャンセルアラートを表示
   Alert.cancelAlert(vc: self, title: "タイトル", message: "任意のメッセージ", handler: { (_) in
     // OKボタンを押した後の処理を書いて下さい。
   })

   // TextField付きアラートを表示
   Alert.textFieldAlert(vc: self, title: "タイトル", message: "任意のメッセージ", placeholder: "任意の文字", securyText: false, handler: { (text) in
     // TextFieldに入力した文字の処理を書いて下さい。
   })

  // 自動で消えるアラートを表示
  Alert.autoCloseAlert(vc: self, title: "タイトル", message: "任意のメッセージ")
0
0
0

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
0
0