LoginSignup
4
5

More than 5 years have passed since last update.

Swift初心者メモ ボタンおしたらアラート表示

Posted at

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()
    }


}
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5