【Swift3】UIAlertControllerのサンプル3選

# alert

## デモ
![alert2.gif](https://qiita-image-store.s3.amazonaws.com/0/135478/b6b1c9bd-21f4-fc97-7cf2-14bef095e468.gif)

## コード
```.swift
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: "アクション１", style: UIAlertActionStyle.default, handler: {
            (action: UIAlertAction!) in
            print("アクション１をタップした時の処理")
        })
        
        let action2 = UIAlertAction(title: "アクション２", style: UIAlertActionStyle.default, handler: {
            (action: UIAlertAction!) in
            print("アクション２をタップした時の処理")
        })
        
        let action3 = UIAlertAction(title: "アクション３", style: UIAlertActionStyle.destructive, handler: {
            (action: UIAlertAction!) in
            print("アクション３をタップした時の処理")
        })
        
        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](https://qiita-image-store.s3.amazonaws.com/0/135478/1213e82a-aa5c-b13c-9ff6-4f0ccaabb8a5.gif)


## コード

```.swift
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: "アクション１", style: UIAlertActionStyle.default, handler: {
            (action: UIAlertAction!) in
            print("アクション１をタップした時の処理")
        })
        
        let action2 = UIAlertAction(title: "アクション２", style: UIAlertActionStyle.default, handler: {
            (action: UIAlertAction!) in
            print("アクション２をタップした時の処理")
        })
        
        let action3 = UIAlertAction(title: "アクション３", style: UIAlertActionStyle.destructive, handler: {
            (action: UIAlertAction!) in
            print("アクション３をタップした時の処理")
        })
        
        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を動的に生成する方法

## コード

```.swift
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)
    }
}
```