17
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Swift】 alert表示のテンプレート

Posted at

タイトル通りです。
入力チェック時のエラー表示や、入力完了時のメッセージ表示の際の、「とりあえずアラートダイアログでメッセージを表示したい(ダイアログのボタン押下による処理は無くてよい)」時に便利に使いまわせるメソッド。

開発環境

端末:MacBook Pro/MacOS 10.14.5(Mojave)
Xcode:10.2.1
Swift:5

実装内容

ソースサンプル

sample.swift
import UIKit

class secondViewController: UIViewController {
    var alertController: UIAlertController!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func alert(title:String, message:String) {
        alertController = UIAlertController(title: title,
                                   message: message,
                                   preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "OK",
                                       style: .default,
                                       handler: nil))
        present(alertController, animated: true)
    }

    @IBAction func tapButton(_ sender: Any) {
        alert(title: "サンプル",
              message: "メッセージ表示")
    }
}

画面表示

Screen Shot 2019-09-13 at 2.52.04.png

17
22
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
17
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?