ボタンのタグを使う

ボタンとラベルの学習を発展させる。
ボタンの形を変えて、タグを使ってみる。

丸いボタンと四角いボタンを作り、押されたボタンによってラベルとテキストを変える。
ボタンにタグをつけて、メソッドでswitchコードを使って場合分けする。

初めてswitchを使ってみた。こんな風に使うのかあ、と感動する。
やっとswitchを使うところまで来たぞ。

出来上がり。
ようやく猿なみかなあ・・・(猿に怒られそう)。

=========================
(備忘録)button.tagの使い方(入門)
コード:Xcode7.2
=========================
//
// ViewController.swift
// ボタンのタグを使う
//
// Created by yasui_swift on 2015/12/18.
// Copyright © 2015年 darumammz.com. All rights reserved.
//
import UIKit
class ViewController: UIViewController {

let button = UIButton(frame:CGRectMake(0,0,100,100))
let button2 = UIButton(frame:CGRectMake(0,0,100,100))
let label:UILabel = UILabel(frame:CGRectMake(0,0,300,50))

override func viewDidLoad() {
super.viewDidLoad()

button.backgroundColor = UIColor.blueColor()
button.layer.position = CGPoint(x:self.view.frame.width/2,y:200)
button.layer.cornerRadius = 50
button.setTitle("丸いボタン", forState: .Normal)
button.tag = 1
button.setTitleColor(UIColor.whiteColor(),forState:.Normal)
self.view.addSubview(button)
button.addTarget(self, action: "push:", forControlEvents: .TouchUpInside)

button2.backgroundColor = UIColor.yellowColor()
button2.layer.position = CGPoint(x:self.view.frame.width/2,y:350)
//button2.layer.cornerRadius = 50
button2.setTitle("四角ボタン", forState: .Normal)
button2.tag = 2
button2.setTitleColor(UIColor.blackColor(),forState:.Normal)
self.view.addSubview(button2)
button2.addTarget(self, action: "push:", forControlEvents: .TouchUpInside)

label.backgroundColor = UIColor.greenColor()
label.layer.borderWidth = 2
label.layer.position = CGPoint(x:self.view.frame.width/2,y:self.view.frame.height*0.7)
label.textColor = UIColor.blackColor()
label.textAlignment = NSTextAlignment.Center
label.font = UIFont.systemFontOfSize(20)
self.view.addSubview(label)

label.text = " ? "
}
func push(sender:UIButton){
switch sender.tag{
case 1:
label.textColor = UIColor.whiteColor()
label.backgroundColor = UIColor.blueColor()
label.text = "丸いボタンが押されました"
case 2:
label.textColor = UIColor.blackColor()
label.backgroundColor = UIColor.yellowColor()
label.text = "四角ボタンが押されました"
default: break
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

=========================