MENU
コンテンツ再構築中

Swift:UIAlertController でアラートを表示するサンプルコード

アプリにアラートを表示するとき、iOS 7 までは UIAlertView を使用していたかと思います。

iOS 8 からは UIAlertViewController が登場し、UIAlertView が非推奨となったため、今後リリースされるアプリは UIAlertController に対応していく必要があります。

今回はこの UIAlertController の使用方法をサンプルコードを交えながら説明したいと思います。

INDEX

UIAlertController について

アラートには UIAlertView でおなじみの Alert と ActionSheet が指定できます。

アクションのスタイル

[code]
UIAlertControllerStyle.Alert // アラート
UIAlertControllerStyle.ActionSheet // アクションシート
[/code]

ボタンの種類は以下の3種類です。Default と Destructive は複数の定義が可能です。

アクションボタンのスタイル

[code]
UIAlertActionStyleDefault // 標準
UIAlertActionStyleCancel // ボールド
UIAlertActionStyleDestructive // 赤色
[/code]

UIAlertController でアラートを表示する手順

アラートを表示する手順です。上から順に定義していくとアラートの表示が完了します。

アクションスタイルの定義

最初にアラートの種類を定義します。Alert(ポップアップするアラート)か ActionSheet(下からニョキっと出るアラート)を選択します。

[code]
// Alert の場合
let alert:UIAlertController = UIAlertController(title:”alert”,
message: “alertView”,
preferredStyle: UIAlertControllerStyle.Alert
)

// ActionSheet の場合
let actionSheet:UIAlertController = UIAlertController(title:”sheet”,
message: “actionSheet”,
preferredStyle: UIAlertControllerStyle.ActionSheet
)
[/code]

アクションボタンの定義

ボタンの種類を定義します。アラート、アクションシートともに定義は同じです。Default と Destructive は複数の定義が可能です。

[code]
// Cancel 一つだけしか指定できない
let cancelAction:UIAlertAction = UIAlertAction(title: “Cancel”,
style: UIAlertActionStyle.Cancel,
handler:{
(action:UIAlertAction!) -> Void in
println(“Cancel”)
})

// Default 複数指定可
let defaultAction:UIAlertAction = UIAlertAction(title: “Default”,
style: UIAlertActionStyle.Default,
handler:{
(action:UIAlertAction!) -> Void in
println(“Default”)
})

// Destructive 複数指定可
let destructiveAction:UIAlertAction = UIAlertAction(title: “Destructive”,
style: UIAlertActionStyle.Destructive,
handler:{
(action:UIAlertAction!) -> Void in
println(“Destructive”)
})

[/code]

アクションを追加

UIAlertController にアクションを追加します。記述した順にアラート画面へ反映されます。

[code]
// Alert
alert.addAction(cancelAction)
alert.addAction(defaultAction)
alert.addAction(destructiveAction)

// ActionSheet
actionSheet.addAction(cancelAction)
actionSheet.addAction(defaultAction)
actionSheet.addAction(destructiveAction)
[/code]

UIAlertController の表示

アラートを表示するタイミングで presentViewController から UIAlertController を呼び出します。

[code]
// Display
presentViewController(alert, animated: true, completion: nil)
presentViewController(actionSheet, animated: true, completion: nil)
[/code]

UIAlertController のサンプルコード

上記のコードを ViewController へ記述したアラートとアクションシートの動作サンプルです。

ビルド結果画面

img_swift-uialertcontroller-sample

ViewController.swift

[code]
import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let alertButton = UIButton()
alertButton.frame = CGRectMake(self.view.frame.width/2-100, self.view.frame.height/2-40, 200, 20)
alertButton.setTitle(“Show Alert”, forState: .Normal)
alertButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
alertButton.addTarget(self, action: “showAlert:”, forControlEvents:.TouchUpInside)
self.view.addSubview(alertButton)

let actionSheetButton = UIButton()
actionSheetButton.frame = CGRectMake(self.view.frame.width/2-100, self.view.frame.height/2+40, 200, 20)
actionSheetButton.setTitle(“ShowActionSheet”, forState: .Normal)
actionSheetButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
actionSheetButton.addTarget(self, action: “showActionSheet:”, forControlEvents:.TouchUpInside)
self.view.addSubview(actionSheetButton)
}

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

// Alert
func showAlert(sender: AnyObject) {

// Style Alert
let alert: UIAlertController = UIAlertController(title:”alert”,
message: “alertController”,
preferredStyle: UIAlertControllerStyle.Alert
)

// Cancel 一つだけしか指定できない
let cancelAction: UIAlertAction = UIAlertAction(title: “Cancel”,
style: UIAlertActionStyle.Cancel,
handler:{
(action:UIAlertAction!) -> Void in
println(“Cancel”)
})

// Default 複数指定可
let defaultAction: UIAlertAction = UIAlertAction(title: “Default”,
style: UIAlertActionStyle.Default,
handler:{
(action:UIAlertAction!) -> Void in
println(“Default”)
})

// Destructive 複数指定可
let destructiveAction: UIAlertAction = UIAlertAction(title: “Destructive”,
style: UIAlertActionStyle.Destructive,
handler:{
(action:UIAlertAction!) -> Void in
println(“Destructive”)
})

// AddAction 記述順に反映される
alert.addAction(cancelAction)
alert.addAction(defaultAction)
alert.addAction(destructiveAction)

// Display
presentViewController(alert, animated: true, completion: nil)

}

// ActionSheet
func showActionSheet(sender: AnyObject) {

// Style ActionSheet
let actionSheet: UIAlertController = UIAlertController(title:”sheet”,
message: “alertController actionSheet”,
preferredStyle: UIAlertControllerStyle.ActionSheet
)

// Cancel 一つだけしか指定できない
let cancelAction: UIAlertAction = UIAlertAction(title: “Cancel”,
style: UIAlertActionStyle.Cancel,
handler:{
(action:UIAlertAction!) -> Void in
println(“Cancel”)
})

// Default 複数指定可
let defaultAction: UIAlertAction = UIAlertAction(title: “Default”,
style: UIAlertActionStyle.Default,
handler:{
(action:UIAlertAction!) -> Void in
println(“Default”)
})

// Destructive 複数指定可
let destructiveAction: UIAlertAction = UIAlertAction(title: “Destructive”,
style: UIAlertActionStyle.Destructive,
handler:{
(action:UIAlertAction!) -> Void in
println(“Destructive”)
})

// AddAction 記述順に反映される
actionSheet.addAction(cancelAction)
actionSheet.addAction(defaultAction)
actionSheet.addAction(destructiveAction)

// Display
presentViewController(actionSheet, animated: true, completion: nil)
}
}
[/code]

まとめ

UIAlertView はボタンが押されたときはデリゲートメソッドで処理していましたが、UIAlertController はクロージャでの処理となりました。

Swift でアラートのコーディングをおこなったとき、UIAlertController の方が記述がシンプルになり、直感的に理解できるようになっています。

レガシー対応のために UIAlertView も必要かと思いますが、昨今の Swift の開発スピードをみる限り、比較的早く UIAlertViewController へ移行していくのではないでしょうか。

この記事がみなさんのお役に立ちましたら、下記「Share it」よりブックマークやSNSで共有していただければ幸いです。

Please share it!
  • URLをコピーしました!
  • URLをコピーしました!
INDEX