window.alert("アラート!");
「わあ プログラミングっぽい」
はじめてjsでアラートを出した時の感動を今でも覚えています。
iOSでもアラートを出したい。
そう思ったのでボタンおしたらアラートをやってみました。
ViewController.swift
import UIKit
class ViewController: UIViewController {
func showAlert() {
// OSのバージョンを取得
let VERSION: Float = (UIDevice.currentDevice().systemVersion as NSString).floatValue
// 8.0以上の場合
if VERSION >= 8.0 {
let alertController = UIAlertController(title: "タイトル", message: "ここにメッセージ", preferredStyle: .Alert)
// OKボタン
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
// それ以外
else {
let alert = UIAlertView()
alert.title = "タイトル"
alert.message = "ここにメッセージ"
alert.addButtonWithTitle("OK")
alert.show()
}
}
// ボタンつくって接続しとく
@IBAction func OnClickBtn(sender: AnyObject) {
showAlert()
}
override func viewDidLoad() {
super.viewDidLoad()
self.showAlert()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}