アラートを実装する(UIAlertAction)

ログアウト画面でよくある以下のようなものを実装する

f:id:kinoue3:20161025001256p:plain


let alertViewController = UIAlertController()
// Alertを実装したい場合は以下のようにする
// let alertViewController = UIAlertController(title: "確認", message: "本当にいいですか?", preferredStyle:  UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "ログアウト", style: .Destructive, handler:{
// ログアウトが押された時の処理をクロージャーで書く
    (action:UIAlertAction!) -> Void in
    print("ログアウト")
})
let cancelAction = UIAlertAction(title: "キャンセル", style: .Cancel, handler: nil)
alertViewController.addAction(okAction)
alertViewController.addAction(cancelAction)
self.presentViewController(alertViewController, animated: true, completion: nil)