Help us understand the problem. What is going on with this article?

アウトレットコレクションを利用する

More than 5 years have passed since last update.

アプリ動作

やったこと

  • アウトレットコレクションを使う
  • 複数のボタンから同じメソッドを呼び出す
  • 複数のボタンにそれぞれタグを付ける

アプリ内容

  • (上部)複数フォームから受け取った値を、コンソール出力する
  • (下部)色のボタンを押したらそれぞれ英語名をラベルに表示する

「詳細Objective-C iPhoneアプリ開発入門ノート」Chapter4-2の学習記録です。

コード

ViewController.swift
class ViewController: UIViewController {

    // テキストボックス配列
    @IBOutlet var Boxes: [UITextField]!

    // 色を英語で言うとラベル
    @IBOutlet weak var ColorLabel: UILabel!
    // 色ボタン
    @IBOutlet var ColorButtons: [UIButton]!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    // 周辺タップでキーボードを閉じる
    @IBAction func EndEdit(sender: UITapGestureRecognizer) {
        self.view.endEditing(true)
    }

    // コレクションを送信する
    @IBAction func SendList(sender: UIButton) {
        var str:String = ""
        for unit:UITextField in self.Boxes {
            str += unit.text + "\n"
        }
        print(str)
    }
    // ラベルに色名を表示する
    @IBAction func touchWordButton(sender: UIButton) {
        var word:NSString = ""
        switch(sender.tag) {
            case 10: word = "Green"
            case 20: word = "Red"
            case 30: word = "Yellow"
            case 40: word = "Blue"
            default: break
        }
        self.ColorLabel.text = word
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

ソースアップ

https://github.com/hanoopy/ios_study_outletcolc

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした