電子の海をたゆたう

iOS初心者がXcodeでアプリ開発を学んでいく日記です。

UIAlertControllerをSwiftで使ってみた

iOS8からUIAlertViewが非推奨なっているとのことで、
UIAlertControllerをSwiftで書いてみました。
新しい手法はどんどん取り入れていって取り残されないようにしていかないとですね。

では、UIAlertControllerとはどういうものなのかというところから
iOS8でSwiftにおけるUIAlertViewが廃止されるため、
UIAlertControllerに切り替える必要があると。
公式から今日からそれ公式のやり方じゃないから、新しいやり方でよろしくねという無茶振り。

UIAlertControllerの特徴

・ボタンの作成はUIAlertActionというクラスを使ってボタンを追加する
・アラートの種類としてはAlertとActionSheetの2種類がある
・ボタンの表示スタイルは3種類

実際のコード

*UIAlertControllerでAlert表示

func showAlert(){
        
        let alertCtr = UIAlertController(title: "Alert",
                                         message: "show alert",
                                         preferredStyle: .Alert)
        
        let cancelAction:UIAlertAction = UIAlertAction(title: "Cancel",
                                                       style: UIAlertActionStyle.Cancel,
                                                       handler:{
                                                        (action:UIAlertAction!) -> Void in
                                                        print("Cancel")
        })
        
        let defaultAction:UIAlertAction = UIAlertAction(title: "Default",
                                                        style: UIAlertActionStyle.Default,
                                                        handler:{
                                                            (action:UIAlertAction!) -> Void in
                                                            print("Default")
        })
        
        let destructiveAction:UIAlertAction = UIAlertAction(title: "Destructive",
                                                            style: UIAlertActionStyle.Destructive,
                                                            handler:{
                                                                (action:UIAlertAction!) -> Void in
                                                                print("Destructive")
        })
        
        alertCtr.addAction(cancelAction)
        alertCtr.addAction(defaultAction)
        alertCtr.addAction(destructiveAction)
        
        presentViewController(alertCtr, animated: true, completion: nil)
    }

*UIAlertControllerでActionShett表示

func showActionSheet(){
        
        let alertCtr = UIAlertController(title: "ActionSheet",
                                         message: "show ActionSheet",
                                         preferredStyle: .ActionSheet)
        
        let cancelAction:UIAlertAction = UIAlertAction(title: "Cancel",
                                                       style: UIAlertActionStyle.Cancel,
                                                       handler:{
                                                        (action:UIAlertAction!) -> Void in
                                                        print("Cancel")
        })
        
        let defaultAction:UIAlertAction = UIAlertAction(title: "Default",
                                                        style: UIAlertActionStyle.Default,
                                                        handler:{
                                                            (action:UIAlertAction!) -> Void in
                                                            print("Default")
        })
        
        let destructiveAction:UIAlertAction = UIAlertAction(title: "Destructive",
                                                            style: UIAlertActionStyle.Destructive,
                                                            handler:{
                                                                (action:UIAlertAction!) -> Void in
                                                                print("Destructive")
        })
        
        alertCtr.addAction(cancelAction)
        alertCtr.addAction(defaultAction)
        alertCtr.addAction(destructiveAction)
        
        presentViewController(alertCtr, animated: true, completion: nil)
    }

UIAlertController宣言時に、preferredStyleをAlertにすることで以下の画像の様にすることができます。

ActionSheetにすることで以下のようにすることが可能です。

ボタンの数はUIAlertActionで増やすことができ、デフォルトで用意されているのが
・UIAlertActionStyle.Cancel (キャンセルボタン)
・UIAlertActionStyle.Default (デフォルトボタン)
・UIAlertActionStyle.Destructive (赤文字ボタン)

の3種類。

個人的に便利だなと感じたのが、preferredStyleのAlertでボタンが2つの時とそれ以上の時は自動でボタンが3つ縦に並ぶのはすごくスマートだと感じました。

簡単な使い方としてはこんな感じです。今回GitHubに公開してみました。
しょぼいコードですが、オープンなところにコードを晒すことで自分のプラスになることが増えるといいなと思います。
github.com