alert
デモ
コード
.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: "アクション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
デモ
コード
.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: "アクション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を動的に生成する方法
コード
.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)
}
}