LoginSignup
25
31

More than 5 years have passed since last update.

【Swift3】UIAlertControllerのサンプル3選

Last updated at Posted at 2016-11-14

alert

デモ

alert2.gif

コード

import UIKit

class ViewController: UIViewController {

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

    @IBAction func showAlert(_ sender: Any) {
        let alert = UIAlertController(title:"タイトル", message: "メッセージ", preferredStyle: UIAlertControllerStyle.alert)

        let action1 = UIAlertAction(title: "アクション1", style: UIAlertActionStyle.default, handler: {
            (action: UIAlertAction!) in
            print("アクション1をタップした時の処理")
        })

        let action2 = UIAlertAction(title: "アクション2", style: UIAlertActionStyle.default, handler: {
            (action: UIAlertAction!) in
            print("アクション2をタップした時の処理")
        })

        let action3 = UIAlertAction(title: "アクション3", style: UIAlertActionStyle.destructive, handler: {
            (action: UIAlertAction!) in
            print("アクション3をタップした時の処理")
        })

        let cancel = UIAlertAction(title: "キャンセル", style: UIAlertActionStyle.cancel, handler: {
            (action: UIAlertAction!) in
            print("キャンセルをタップした時の処理")
        })

        alert.addAction(action1)
        alert.addAction(action2)
        alert.addAction(action3)
        alert.addAction(cancel)

        self.present(alert, animated: true, completion: nil)
    }

}

actionSheet

デモ

alert.gif

コード

import UIKit

class ViewController: UIViewController {

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

    @IBAction func showAlert(_ sender: Any) {
        let actionSheet = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: UIAlertControllerStyle.actionSheet)

        let action1 = UIAlertAction(title: "アクション1", style: UIAlertActionStyle.default, handler: {
            (action: UIAlertAction!) in
            print("アクション1をタップした時の処理")
        })

        let action2 = UIAlertAction(title: "アクション2", style: UIAlertActionStyle.default, handler: {
            (action: UIAlertAction!) in
            print("アクション2をタップした時の処理")
        })

        let action3 = UIAlertAction(title: "アクション3", style: UIAlertActionStyle.destructive, handler: {
            (action: UIAlertAction!) in
            print("アクション3をタップした時の処理")
        })

        let cancel = UIAlertAction(title: "キャンセル", style: UIAlertActionStyle.cancel, handler: {
            (action: UIAlertAction!) in
            print("キャンセルをタップした時の処理")
        })

        actionSheet.addAction(action1)
        actionSheet.addAction(action2)
        actionSheet.addAction(action3)
        actionSheet.addAction(cancel)

        self.present(actionSheet, animated: true, completion: nil)
    }

}

actionを動的に生成する方法

コード

import UIKit

class ViewController: UIViewController {

    var yamaguchiMaho: [String] = []

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

    @IBAction func showAlert(_ sender: Any) {
        let alert = UIAlertController(title:"山口真帆", message: "例の動画", preferredStyle: UIAlertControllerStyle.alert)
        yamaguchiMaho = ["ハレンチ", "写真集", "ナマ配信"]

            for harenchi in yamaguchiMaho {
                let harenchi = UIAlertAction(title: harenchi, style: UIAlertActionStyle.default, handler: {
                    (action: UIAlertAction!) in
                    print(harenchi)
                })
                alert.addAction(harenchi)
            }

        let cancel = UIAlertAction(title: "キャンセル", style: UIAlertActionStyle.cancel, handler: {
            (action: UIAlertAction!) in
            print("キャンセルをタップした時の処理")
        })
        alert.addAction(cancel)
        self.present(alert, animated: true, completion: nil)
    }
}
25
31
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
25
31