如何处理Swift中许多UIButton的点击

时间:2014-09-14 05:32:00

标签: ios swift uibutton

我正在尝试在Swift中创建一个应用程序,它有一个带有单个数字整数的标签网格作为标签文本。我想点击一个标签并获得整数增量。我从一个关于Tic Tac Toe app的教程中学到了我可以用一个清除按钮(1-36)覆盖每个标签并使用水龙头增加标签(1 -36)其下的文字编号。到现在为止还挺好。本教程使用switch语句传递轻敲按钮的标记,并将案例硬编码到Label引用。这对Tic Tac Toe来说很好,因为它只是一个3x3的网格,但我的网格是6x6而且我懒得硬编码所有36个案例。是否有更智能的方法来传递按钮标签并执行此操作?

我正在考虑将按钮标签文本整数存储在数组中并读取/写入/然后从那里更新标签。这可能比我现在的要好,但我认为它不能解决我的switch语句问题。

任何建议都将受到赞赏。

import UIKit

class ViewController: UIViewController {

@IBOutlet var Label1: UILabel! //Row one
@IBOutlet var Label2: UILabel!
@IBOutlet var Label3: UILabel!
@IBOutlet var Label4: UILabel!
@IBOutlet var Label5: UILabel!
@IBOutlet var Label6: UILabel!

@IBOutlet var Label7: UILabel!  //Row two
@IBOutlet var Label8: UILabel!
@IBOutlet var Label9: UILabel!
@IBOutlet var Label10: UILabel!
@IBOutlet var Label11: UILabel!
@IBOutlet var Label12: UILabel!

@IBOutlet var Label13: UILabel!  //Row three
@IBOutlet var Label14: UILabel!
@IBOutlet var Label15: UILabel!
@IBOutlet var Label16: UILabel!
@IBOutlet var Label17: UILabel!
@IBOutlet var Label18: UILabel!

@IBOutlet var Label19: UILabel!  //Row four
@IBOutlet var Label20: UILabel!
@IBOutlet var Label21: UILabel!
@IBOutlet var Label22: UILabel!
@IBOutlet var Label23: UILabel!
@IBOutlet var Label24: UILabel!

@IBOutlet var Label25: UILabel!  //Row five
@IBOutlet var Label26: UILabel!
@IBOutlet var Label27: UILabel!
@IBOutlet var Label28: UILabel!
@IBOutlet var Label29: UILabel!
@IBOutlet var Label30: UILabel!

@IBOutlet var Label31: UILabel!  //Row six
@IBOutlet var Label32: UILabel!
@IBOutlet var Label33: UILabel!
@IBOutlet var Label34: UILabel!
@IBOutlet var Label35: UILabel!
@IBOutlet var Label36: UILabel!


@IBOutlet var Button1: UIButton!  //Row one
@IBOutlet var Button2: UIButton!
@IBOutlet var Button3: UIButton!
@IBOutlet var Button4: UIButton!
@IBOutlet var Button5: UIButton!
@IBOutlet var Button6: UIButton!

@IBOutlet var Button7: UIButton!  //Row two
@IBOutlet var Button8: UIButton!
@IBOutlet var Button9: UIButton!
@IBOutlet var Button10: UIButton!
@IBOutlet var Button11: UIButton!
@IBOutlet var Button12: UIButton!

@IBOutlet var Button13: UIButton!  //Row three
@IBOutlet var Button14: UIButton!
@IBOutlet var Button15: UIButton!
@IBOutlet var Button16: UIButton!
@IBOutlet var Button17: UIButton!
@IBOutlet var Button18: UIButton!

@IBOutlet var Button19: UIButton!  //Row four
@IBOutlet var Button20: UIButton!
@IBOutlet var Button21: UIButton!
@IBOutlet var Button22: UIButton!
@IBOutlet var Button23: UIButton!
@IBOutlet var Button24: UIButton!

@IBOutlet var Button25: UIButton!  //Row five
@IBOutlet var Button26: UIButton!
@IBOutlet var Button27: UIButton!
@IBOutlet var Button28: UIButton!
@IBOutlet var Button29: UIButton!
@IBOutlet var Button30: UIButton!

@IBOutlet var Button31: UIButton!  //Row six
@IBOutlet var Button32: UIButton!
@IBOutlet var Button33: UIButton!
@IBOutlet var Button34: UIButton!
@IBOutlet var Button35: UIButton!
@IBOutlet var Button36: UIButton!

@IBAction func UIButtonClicked(sender:UIButton) {
    setLabelTextForButton(sender.tag)
}

func setLabelTextForButton(labelTag:Int) {
    switch labelTag{
    case 1:
        var currentLabel:Int = Label1.text.toInt()!
        if currentLabel == 9        // IF statement to reset to 0 if label is currently 9
        { currentLabel = 0
            Label1.text = String(currentLabel) }
        else {
        currentLabel = currentLabel + 1
            Label1.text = String(currentLabel)}
    case 2:
        var currentLabel:Int = Label2.text.toInt()! // OMG I have to code for all these buttons
        currentLabel = currentLabel + 1
        Label2.text = String(currentLabel)
    case 3:
        var currentLabel:Int = Label3.text.toInt()!
        currentLabel = currentLabel + 1
        Label3.text = String(currentLabel)
    case 4:
        var currentLabel:Int = Label4.text.toInt()!
        currentLabel = currentLabel + 1
        Label4.text = String(currentLabel)
    case 5:
        var currentLabel:Int = Label5.text.toInt()!
        currentLabel = currentLabel + 1
        Label5.text = String(currentLabel)
    case 6:
        var currentLabel:Int = Label6.text.toInt()!
        currentLabel = currentLabel + 1
        Label6.text = String(currentLabel)
    case 7:
        var currentLabel:Int = Label7.text.toInt()!
        currentLabel = currentLabel + 1
        Label7.text = String(currentLabel)
    case 8:
        var currentLabel:Int = Label8.text.toInt()!
        currentLabel = currentLabel + 1
        Label8.text = String(currentLabel)
    case 9:
        var currentLabel:Int = Label9.text.toInt()!
        currentLabel = currentLabel + 1
        Label9.text = String(currentLabel)
    case 10:
        var currentLabel:Int = Label10.text.toInt()!
        currentLabel = currentLabel + 1
        Label10.text = String(currentLabel)
    case 11:
        var currentLabel:Int = Label11.text.toInt()!
        currentLabel = currentLabel + 1
        Label11.text = String(currentLabel)
    case 12:
        var currentLabel:Int = Label12.text.toInt()!
        currentLabel = currentLabel + 1
        Label12.text = String(currentLabel)
    case 13:
        var currentLabel:Int = Label13.text.toInt()!
        currentLabel = currentLabel + 1
        Label13.text = String(currentLabel)
    case 14:
        var currentLabel:Int = Label14.text.toInt()!
        currentLabel = currentLabel + 1
        Label14.text = String(currentLabel)
    case 15:
        var currentLabel:Int = Label15.text.toInt()!
        currentLabel = currentLabel + 1
        Label15.text = String(currentLabel)
    case 16:
        var currentLabel:Int = Label16.text.toInt()!
        currentLabel = currentLabel + 1
        Label16.text = String(currentLabel)
    case 17:
        var currentLabel:Int = Label17.text.toInt()!
        currentLabel = currentLabel + 1
        Label17.text = String(currentLabel)
    case 18:
        var currentLabel:Int = Label18.text.toInt()!
        currentLabel = currentLabel + 1
        Label18.text = String(currentLabel)
    case 19:
        var currentLabel:Int = Label19.text.toInt()!
        currentLabel = currentLabel + 1
        Label19.text = String(currentLabel)
    case 20:
        var currentLabel:Int = Label20.text.toInt()!
        currentLabel = currentLabel + 1
        Label20.text = String(currentLabel)
    case 21:
        var currentLabel:Int = Label21.text.toInt()!
        currentLabel = currentLabel + 1
        Label21.text = String(currentLabel)
    case 22:
        var currentLabel:Int = Label22.text.toInt()!
        currentLabel = currentLabel + 1
        Label22.text = String(currentLabel)
    case 23:
        var currentLabel:Int = Label23.text.toInt()!
        currentLabel = currentLabel + 1
        Label23.text = String(currentLabel)
    case 24:
        var currentLabel:Int = Label24.text.toInt()!
        currentLabel = currentLabel + 1
        Label24.text = String(currentLabel)
    case 25:
        var currentLabel:Int = Label25.text.toInt()!
        currentLabel = currentLabel + 1
        Label25.text = String(currentLabel)
    case 26:
        var currentLabel:Int = Label26.text.toInt()!
        currentLabel = currentLabel + 1
        Label26.text = String(currentLabel)
    case 27:
        var currentLabel:Int = Label27.text.toInt()!
        currentLabel = currentLabel + 1
        Label27.text = String(currentLabel)
    case 28:
        var currentLabel:Int = Label28.text.toInt()!
        currentLabel = currentLabel + 1
        Label28.text = String(currentLabel)
    case 29:
        var currentLabel:Int = Label29.text.toInt()!
        currentLabel = currentLabel + 1
        Label29.text = String(currentLabel)
    case 30:
        var currentLabel:Int = Label30.text.toInt()!
        currentLabel = currentLabel + 1
        Label30.text = String(currentLabel)
    case 31:
        var currentLabel:Int = Label31.text.toInt()!
        currentLabel = currentLabel + 1
        Label31.text = String(currentLabel)
    case 32:
        var currentLabel:Int = Label32.text.toInt()!
        currentLabel = currentLabel + 1
        Label32.text = String(currentLabel)
    case 33:
        var currentLabel:Int = Label33.text.toInt()!
        currentLabel = currentLabel + 1
        Label33.text = String(currentLabel)
    case 34:
        var currentLabel:Int = Label34.text.toInt()!
        currentLabel = currentLabel + 1
        Label34.text = String(currentLabel)
    case 35:
        var currentLabel:Int = Label35.text.toInt()!
        currentLabel = currentLabel + 1
        Label35.text = String(currentLabel)
    case 36:
        var currentLabel:Int = Label36.text.toInt()!
        currentLabel = currentLabel + 1
        Label36.text = String(currentLabel)

    default: something something

1 个答案:

答案 0 :(得分:1)

使用按钮的label属性而不是额外的标签。这看起来像这样:

@IBOutlet var Button1: UIButton!  //Row one
@IBOutlet var Button2: UIButton!
@IBOutlet var Button3: UIButton!
@IBOutlet var Button4: UIButton!
@IBOutlet var Button5: UIButton!
@IBOutlet var Button6: UIButton!
....

@IBAction func increaseLabel(sender: UIButton) {
    var currentLabel = sender.titleLabel.text.toInt()!
    currentLabel = currentLabel + 1
    sender.setTitle("\(currentLabel)", forState: .Normal)
}

然后在Interface Builder中将所有按钮连接到该操作。