如何隐藏按钮(来自for循环)?

时间:2016-11-23 22:27:30

标签: ios swift uibutton

我在for循环中创建了多个按钮(12!)。 现在我会隐藏其中一个按钮。

但我不知道如何;)

我还使用了button.tag来获取我触摸的每个按钮的标签,以填充另一个标签中的文字。

BTW:我用它来进行PinCode检查,我想隐藏OK_Button,直到PinCode正确。

var pinCode = [Int]()
var pinCodeCounter = 0


func pinCodeLabel() {
    pinCodeCounter += 1
    if pinCodeCounter == 1 {
        pinLabel.text = "*"
    }
    if pinCodeCounter == 2 {
    pinLabel.text = "**"
    }
    if pinCodeCounter == 3 {
        pinLabel.text = "***"
    }
    if pinCodeCounter == 4 {
        pinLabel.text = "****"
    }
    if pinCodeCounter == 0 {
        pinLabel.text = ""
    }
}

@IBOutlet weak var pinLabel: UILabel!
@IBOutlet weak var pinCodeCheck: UILabel!
@IBOutlet weak var continueToTimeControlView: UIButton!


@IBAction func pinCodeCorrect() {
    if pinCode == [8, 1, 1, 8] {
        pinCodeCheck.text = "PIN-Code Richtig"
        pinCodeCheck.textColor = UIColor.black
        continueToTimeControlView.isHidden = false
    } else {
        pinCodeCheck.text = "PIN-Code Falsch"
        pinCodeCheck.textColor = UIColor.red
    }

 }

let btn_create = UIButton(type: .system)
@IBOutlet weak var attendanceView: UIView!

func btnAction(sender: UIButton!) {
    switch (sender.tag) {
    case 0:
        pinCode.append(1)
        print("1")
    case 1:
        pinCode.append(2)
        print("2")
    case 2:
        pinCode.append(3)
        print("3")
    case 3:
        pinCode.append(4)
        print("4")
    case 4:
        pinCode.append(5)
        print("5")
    case 5:
        pinCode.append(6)
        print("6")
    case 6:
        pinCode.append(7)
        print("7")
    case 7:
        pinCode.append(8)
        print("8")
    case 8:
        pinCode.append(9)
        print("9")
    case 9:
        pinCode.removeAll()
        print("<-")
    case 10:
        pinCode.append(0)
        print("0")
    case 11:
        pinCodeCorrect()
        print(pinCode)
        print("OK")
    default:
        print("")
    }
    pinCodeCorrect()
}


override func viewDidLoad() {
    super.viewDidLoad()

    continueToTimeControlView.isHidden = true
    var x_axis = 37
    var y_axis = 225
    var z = 0
    var rangeNumbers = ["1","2","3","4","5","6","7","8","9","<-    ","0","OK"]
    var btn_create = UIButton();

    for _ in 1...4 {
        for _ in 1...3 {
            btn_create = UIButton(frame: CGRect(x: x_axis, y: y_axis, width: 90, height: 90))
            btn_create.setTitle(rangeNumbers[z], for: .normal)
            //btn_create?.backgroundColor = UIColor.lightGray
            btn_create.setTitleColor(UIColor.black, for: .normal)
            btn_create.layer.borderColor = UIColor.lightGray.cgColor
            btn_create.layer.borderWidth = 1
            btn_create.layer.cornerRadius = 45
            btn_create.tag = z
            btn_create.addTarget(attendanceView, action:    #selector(btnAction), for: .touchUpInside)
            self.view.addSubview(btn_create)
            x_axis += 105
            z += 1
        }
        x_axis = 37
        y_axis += 100
    }
}

1 个答案:

答案 0 :(得分:-1)

var pinCode = [Int]()
var pinCodeCounter = 0


func pinCodeLabel() {
    pinCodeCounter += 1
    if pinCodeCounter == 1 {
        pinLabel.text = "*"
    }
    if pinCodeCounter == 2 {
        pinLabel.text = "**"
    }
    if pinCodeCounter == 3 {
        pinLabel.text = "***"
    }
    if pinCodeCounter == 4 {
        pinLabel.text = "****"
    }
    if pinCodeCounter == 0 {
        pinLabel.text = ""
    }
}

@IBOutlet weak var pinLabel: UILabel!
@IBOutlet weak var pinCodeCheck: UILabel!
@IBOutlet weak var continueToTimeControlView: UIButton!


@IBAction func pinCodeCorrect() {
    if pinCode == [8, 1, 1, 8] {
        pinCodeCheck.text = "PIN-Code Richtig"
        pinCodeCheck.textColor = UIColor.black
        continueToTimeControlView.isHidden = false
    } else {
        pinCodeCheck.text = "PIN-Code Falsch"
        pinCodeCheck.textColor = UIColor.red
    }

}

let btn_create = UIButton(type: .system)
@IBOutlet weak var attendanceView: UIView!

func btnAction(sender: UIButton!) {
    switch (sender.tag) {
    case 0:
        pinCode.append(1)
        print("1")
    case 1:
        pinCode.append(2)
        print("2")
    case 2:
        pinCode.append(3)
        print("3")
    case 3:
        pinCode.append(4)
        print("4")
    case 4:
        pinCode.append(5)
        print("5")
    case 5:
        pinCode.append(6)
        print("6")
    case 6:
        pinCode.append(7)
        print("7")
    case 7:
        pinCode.append(8)
        print("8")
    case 8:
        pinCode.append(9)
        print("9")
    case 9:
        pinCode.removeAll()
        print("<-")
    case 10:
        pinCode.append(0)
        print("0")
    case 11:
        pinCodeCorrect()
        print(pinCode)
        print("OK")
    default:
        print("")
    }
    pinCodeCorrect()
}


override func viewDidLoad() {
    super.viewDidLoad()

    continueToTimeControlView.isHidden = true
    var x_axis = 37
    var y_axis = 225
    var z = 0
    var rangeNumbers = ["1","2","3","4","5","6","7","8","9","<-","0","OK"]
    var btn_create = UIButton();

    for _ in 1...4 {
        for _ in 1...3 {
            btn_create = UIButton(frame: CGRect(x: x_axis, y: y_axis, width: 90, height: 90))
            btn_create.setTitle(rangeNumbers[z], for: .normal)
            //btn_create?.backgroundColor = UIColor.lightGray
            btn_create.setTitleColor(UIColor.black, for: .normal)
            btn_create.layer.borderColor = UIColor.lightGray.cgColor
            btn_create.layer.borderWidth = 1
            btn_create.layer.cornerRadius = 45
            btn_create.tag = z
            btn_create.addTarget(attendanceView, action: #selector(btnAction), for: .touchUpInside)
            self.view.addSubview(btn_create)
            x_axis += 105
            z += 1
        }
        x_axis = 37
        y_axis += 100
    }
}

}