LoginSignup
0
1

アラートテンプレ集(コピペおけ)

Last updated at Posted at 2023-11-13

実際によく使う順にアラート(UIAlertController)のテンプレをご用意しました。最後の全体コードをあなたのXcodeプロジェクトに新しくswiftファイルを作ってコピペすれば、すぐにご利用頂けます。

目次

タスク完了アラート
エラー発生アラート
短文説明アラート
二択(はいorいいえ)アラート
入力欄付きアラート
文字数オーバーアラート
入力破棄して画面巻き戻しアラート
全体コード

タスク完了アラート

AlertExtension.swift
static func taskDoneAlert(taskString: String, viewController: UIViewController, completion: (() -> Void)?) {
        let alert = UIAlertController(title: "\(taskString)完了", message: "", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default) { action in
            completion?()
        }
        alert.addAction(okAction)
        viewController.present(alert, animated: true)
    }

使用例↓

SampleViewController.swift
UIAlertController.taskDoneAlert(taskString: "保存", viewController: self) {
            print("保存できましためでたしめでたし")
        }

Simulator Screenshot - iPhone 15 Pro Max - 2023-11-12 at 23.53.52.png

エラー発生アラート

AlertExtension.swift
static func errorAlert(viewController: UIViewController) {
        let alert = UIAlertController(title: "エラー", message: "お手数ですがやり直してみてください\n申し訳ございません", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default)
        alert.addAction(okAction)
        viewController.present(alert, animated: true)
    }

Simulator Screenshot - iPhone 15 Pro Max - 2023-11-13 at 00.23.03.png
catch文とかでエラー起きたときに使ってます。

短文説明アラート

AlertExtension.swift
static func simpleDescriptionAlert(title: String, message: String, viewController: UIViewController) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let ok = UIAlertAction(title: "OK", style: .default)
        alert.addAction(ok)
        viewController.present(alert, animated: true)
    }

使用例↓

SampleViewController.swift
UIAlertController.simpleDescriptionAlert(title: "お問い合わせ先", message: "090-1234-5678", viewController: self)

Simulator Screenshot - iPhone 15 Pro Max - 2023-11-13 at 00.25.09.png

表示後に特に何も処理を走らせず、ただ説明だけしたいときによく使います。

二択アラート

AlertExtension.swift
static func yesOrNoAlert(alertTitle: String, alertMessage: String, viewController: UIViewController, yesAlertStyle: UIAlertAction.Style,
yesCompletion: ( @escaping () -> Void), noCompletion: ( @escaping () -> Void) ) {

        let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
        let yes = UIAlertAction(title: "はい", style: yesAlertStyle) { _ in
            yesCompletion()
        }
        let no = UIAlertAction(title: "いいえ", style: .cancel) { _ in
            noCompletion()
        }
        alert.addAction(yes)
        alert.addAction(no)
        viewController.present(alert, animated: true)
    }

使用例↓

SampleViewController.swift
UIAlertController.yesOrNoAlert(alertTitle: "利用規約を開きますか?", alertMessage: "",
viewController: self, yesAlertStyle: .default) {
            print("ここに規約を開く処理")
        } noCompletion: {
            print("ここに規約を開かない場合の処理")
        }

Simulator Screenshot - iPhone 15 Pro Max - 2023-11-13 at 00.25.00.png

入力欄付きアラート

AlertExtension.swift
static func textFieldAlert(alertTitle: String, alertMessage: String, placeholderText: String,
keyboardType: UIKeyboardType, doneButtonTitle: String, viewController: UIViewController, completion: ( @escaping (String) -> Void)) {

        let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
        alert.addTextField { (textField) in
            textField.placeholder = placeholderText
            textField.keyboardType = keyboardType
        }
        let done = UIAlertAction(title: doneButtonTitle, style: .default) { _ in
            guard let textField = alert.textFields?.first, let inputText = textField.text else { return }
            if inputText == "" {
                UIAlertController.errorAlert(viewController: viewController)
            } else {
                completion(inputText)
            }
        }
        let cancel = UIAlertAction(title: "キャンセル", style: .cancel)
        alert.addAction(done)
        alert.addAction(cancel)
        viewController.present(alert, animated: true)
    }

使用例↓

SampleViewController.swift
UIAlertController.textFieldAlert(alertTitle: "通報しますか?", alertMessage: "何があったか入力してください", placeholderText: "",
keyboardType: .default, doneButtonTitle: "送信", viewController: self) { text in
            print("ここに入力欄の値textをどこかしらに送信する処理")
        }

Simulator Screenshot - iPhone 15 Pro Max - 2023-11-13 at 00.25.57.png

SNSなどユーザー同士でコミュニケーションとれるアプリで実装しないとAppStoreの審査に引っかかってしまうので、よくこのアラートを通報機能として使ってます。

keyboardTypeに.numberPadを指定すれば、画面遷移直前にユーザーに暗証番号を入力してもらうという使い方もできます。

文字数オーバーアラート

AlertExtension.swift
static func wordCoundOverAlert(wordLimit: Int, viewController: UIViewController) {
        let alert = UIAlertController(title: "\(wordLimit)字までしか\n入力できません", message: "", preferredStyle: .alert)
        let ok = UIAlertAction(title: "OK", style: .default)
        alert.addAction(ok)
        viewController.present(alert, animated: true)
    }

使用例↓

SampleViewController.swift
UIAlertController.wordCoundOverAlert(wordLimit: 1000, viewController: self)

Simulator Screenshot - iPhone 15 Pro Max - 2023-11-13 at 00.27.27.png

入力破棄して画面巻き戻しアラート

AlertExtension.swift
static func checkBackAlert(task: String, viewController: UIViewController) {
        let alert = UIAlertController(title: "戻りますか?", message: "\(task)は破棄されます", preferredStyle: .alert)
        let yes = UIAlertAction(title: "はい", style: .destructive) { _ in
            viewController.navigationController?.popViewController(animated: true)
        }
        let no = UIAlertAction(title: "いいえ", style: .cancel)
        alert.addAction(yes)
        alert.addAction(no)
        viewController.present(alert, animated: true)
    }

使用例↓

SampleViewController.swift
UIAlertController.checkBackAlert(task: "入力内容", viewController: self)

Simulator Screenshot - iPhone 15 Pro Max - 2023-11-13 at 00.27.40.png

入力された内容が保存されていないのにUINavigationControllerで前の画面に戻ろうとしている時に使っています。navigationItem.leftBarButtonItemに新しくキャンセルボタンを作っておいて、タップしたときに入力内容が保存されていない場合このメソッドを呼び出します。

削除が関わる選択なので、UIAlertActionのstyleを.destructiveにして赤くしています

全体コード

AlertExtension.swift
import UIKit

extension UIAlertController {

    static func taskDoneAlert(taskString: String, viewController: UIViewController, completion: (() -> Void)?) {
        let alert = UIAlertController(title: "\(taskString)完了", message: "", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default) { action in
            completion?()
        }
        alert.addAction(okAction)
        viewController.present(alert, animated: true)
    }

    
    static func errorAlert(viewController: UIViewController) {
        let alert = UIAlertController(title: "エラー", message: "お手数ですがやり直してみてください\n申し訳ございません", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default)
        alert.addAction(okAction)
        viewController.present(alert, animated: true)
    }
    
    
    static func simpleDescriptionAlert(title: String, message: String, viewController: UIViewController) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let ok = UIAlertAction(title: "OK", style: .default)
        alert.addAction(ok)
        viewController.present(alert, animated: true)
    }
    
    
    static func yesOrNoAlert(alertTitle: String, alertMessage: String, viewController: UIViewController, yesAlertStyle: UIAlertAction.Style, 
    yesCompletion: ( @escaping () -> Void), noCompletion: ( @escaping () -> Void) ) {
    
        let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
        let yes = UIAlertAction(title: "はい", style: yesAlertStyle) { _ in
            yesCompletion()
        }
        let no = UIAlertAction(title: "いいえ", style: .cancel) { _ in
            noCompletion()
        }
        alert.addAction(yes)
        alert.addAction(no)
        viewController.present(alert, animated: true)
    }

    
    static func textFieldAlert(alertTitle: String, alertMessage: String, placeholderText: String,
    keyboardType: UIKeyboardType, doneButtonTitle: String, viewController: UIViewController, completion: ( @escaping (String) -> Void)) {
    
        let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
        alert.addTextField { (textField) in
            textField.placeholder = placeholderText
            textField.keyboardType = keyboardType
        }
        let done = UIAlertAction(title: doneButtonTitle, style: .default) { _ in
            guard let textField = alert.textFields?.first, let inputText = textField.text else { return }
            if inputText == "" {
                UIAlertController.errorAlert(viewController: viewController)
            } else {
                completion(inputText)
            }
        }
        let cancel = UIAlertAction(title: "キャンセル", style: .cancel)
        alert.addAction(done)
        alert.addAction(cancel)
        viewController.present(alert, animated: true)
    }

    
    static func wordCoundOverAlert(wordLimit: Int, viewController: UIViewController) {
        let alert = UIAlertController(title: "\(wordLimit)字までしか\n入力できません", message: "", preferredStyle: .alert)
        let ok = UIAlertAction(title: "OK", style: .default)
        alert.addAction(ok)
        viewController.present(alert, animated: true)
    }

    
    static func checkBackAlert(task: String, viewController: UIViewController) {
        let alert = UIAlertController(title: "戻りますか?", message: "\(task)は破棄されます", preferredStyle: .alert)
        let yes = UIAlertAction(title: "はい", style: .destructive) { _ in
            viewController.navigationController?.popViewController(animated: true)
        }
        let no = UIAlertAction(title: "いいえ", style: .cancel)
        alert.addAction(yes)
        alert.addAction(no)
        viewController.present(alert, animated: true)
    }
    
}
0
1
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
1