Swiftを写経9

今日の写経

010 UIAlertControllerでアラートを表示 - Swift Docs

所感

今回はスムーズに写経できた。

画面キャプチャ

f:id:novlessn:20150809185437p:plain

f:id:novlessn:20150809185443p:plain

ソースコード

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Viewの背景をCyanに設定する
        self.view.backgroundColor = UIColor.cyanColor()
        
        // BUttonの定義をする
        let myButton: UIButton = UIButton()
        myButton.frame = CGRectMake(0, 0, 200, 40);
        myButton.backgroundColor = UIColor.redColor()
        myButton.layer.masksToBounds = true
        myButton.setTitle("UIAlertを発動", forState: UIControlState.Normal)
        myButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        myButton.layer.cornerRadius = 20.0
        myButton.layer.position = CGPoint(x: self.view.frame.width/2, y: 200)
        myButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside)
        
        // ボタンをViewに追加する
        self.view.addSubview(myButton)
    }
    
    /*
    ボタンイベント
    */
    internal func onClickMyButton(sender: UIButton){
        
        // UIAlertControllerを作成する
        let myAlert: UIAlertController = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .Alert)
        let myOkAction = UIAlertAction(title: "OK", style: .Default) { action in
            println("Action OK!")
        }
        myAlert.addAction(myOkAction)
        presentViewController(myAlert, animated: true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}