Ajp
Ajp

Reputation: 333

Text on UIButton not updating SWIFT

I am making an app in Swift on which all elements are added programatically.

//This code is inside of viewDidLoad function
makeButtonWithName(button: answer0B, title: "0", font: 
"HelveticaNeue", fontSize: resetHeight, frame: CGRect(x:width/2 - 
viewWidth/2, y: firstViewY, width: viewWidth, height: viewHeight), 
selector: #selector(self.answer0(_:)))

makeButtonWithName(button: answer1B, title: "0", font: "HelveticaNeue", 
fontSize: resetHeight, frame: CGRect(x:width/2 - viewWidth/2, y: 
secondViewY, width: viewWidth, height: viewHeight), selector: 
#selector(self.answer1(_:)))

makeButtonWithName(button: answer2B, title: "0", font: 
"HelveticaNeue", fontSize: resetHeight, frame: CGRect(x:width/2 - 
viewWidth/2, y: thirdViewY, width: viewWidth, height: viewHeight), 
selector: #selector(self.answer2(_:)))

makeButtonWithName(button: answer3B, title: "0", font: 
"HelveticaNeue", fontSize: resetHeight, frame: CGRect(x:width/2 - 
viewWidth/2, y: fourthViewY, width: viewWidth, height: viewHeight), 
selector: #selector(self.answer3(_:)))


firstNumber = Int(arc4random_uniform(9))
secondNumber = Int(arc4random_uniform(9))
incorrectAnswer1 = Int(arc4random_uniform(18))
incorrectAnswer2 = Int(arc4random_uniform(18))
incorrectAnswer3 = Int(arc4random_uniform(18))
timer.invalidate()
seconds = 31
runTimer()
randomNumbers()

//End viewDidLoad function


func answer0(_ sender: UIButton!){
    let a:Int? = Int((answer0B.titleLabel?.text)!)

    if a == answerNumber{
        correctIncorrectLabel.text = "Correct"
        correctIncorrectLabel.textColor = UIColor.green
        correctNumber += 1
    }
    else{
        correctIncorrectLabel.text = "Incorrect"
        correctIncorrectLabel.textColor = UIColor.red
    }
    randomNumbers()

}

func answer1(_ sender: UIButton!){
    let b:Int? = Int((answer1B.titleLabel?.text)!)

    if b == answerNumber{
        correctIncorrectLabel.text = "Correct"
        correctIncorrectLabel.textColor = UIColor.green
        correctNumber += 1

    }
    else{
        correctIncorrectLabel.text = "Incorrect"
        correctIncorrectLabel.textColor = UIColor.red
    }
    randomNumbers()

}

func answer2(_ sender: UIButton!){
    let c:Int? = Int((answer2B.titleLabel?.text)!)

    if c == answerNumber{
        correctIncorrectLabel.text = "Correct"
        correctIncorrectLabel.textColor = UIColor.green

        correctNumber += 1

    }
    else{
        correctIncorrectLabel.text = "Incorrect"
        correctIncorrectLabel.textColor = UIColor.red
    }
    randomNumbers()
}
func answer3(_ sender: UIButton!){
    let d:Int? = Int((answer3B.titleLabel?.text)!)

    if d == answerNumber{
        correctIncorrectLabel.text = "Correct"
        correctIncorrectLabel.textColor = UIColor.green
        correctNumber += 1

    }
    else{
        correctIncorrectLabel.text = "Incorrect"
        correctIncorrectLabel.textColor = UIColor.red
    }
    randomNumbers()
    printProblem()
}



func randomNumbers(){
    firstNumber = Int(arc4random_uniform(9))
    secondNumber = Int(arc4random_uniform(9))
    answerNumber = firstNumber + secondNumber
    printProblem()

    randomButton = Int(arc4random_uniform(4))
    incorrectAnswer1 = Int(arc4random_uniform(18))
    incorrectAnswer2 = Int(arc4random_uniform(18))
    incorrectAnswer3 = Int(arc4random_uniform(18))
    showTextOnButton()
    totalCorrect.text = "Total Correct: \(correctNumber)"

}

func showTextOnButton(){
    if randomButton == 0 {
        answer0B.setTitle("\(answerNumber)", for: .normal)
        answer1B.setTitle("\(incorrectAnswer1)", for: .normal)
        answer2B.setTitle("\(incorrectAnswer2)", for: .normal)
        answer3B.setTitle("\(incorrectAnswer3)", for: .normal)
    }
    if randomButton == 1 {
        answer1B.setTitle("\(answerNumber)", for: .normal)
        answer0B.setTitle("\(incorrectAnswer1)", for: .normal)
        answer2B.setTitle("\(incorrectAnswer2)", for: .normal)
        answer3B.setTitle("\(incorrectAnswer3)", for: .normal)
    }

    if randomButton == 2 {
        answer1B.setTitle("\(answerNumber)", for: .normal)
        answer2B.setTitle("\(incorrectAnswer1)", for: .normal)
        answer0B.setTitle("\(incorrectAnswer2)", for: .normal)
        answer3B.setTitle("\(incorrectAnswer3)", for: .normal)
    }

    if randomButton == 3 {
        answer1B.setTitle("\(answerNumber)", for: .normal)
        answer2B.setTitle("\(incorrectAnswer1)", for: .normal)
        answer3B.setTitle("\(incorrectAnswer2)", for: .normal)
        answer0B.setTitle("\(incorrectAnswer3)", for: .normal)
    }

}


func printProblem(){
    questionLabel.text = "\(firstNumber) + \(secondNumber)"
}

func makeButtonWithName(button: UIButton,title: String,  font: String, fontSize: Int, frame: CGRect, selector: Selector) {

    let button = UIButton(type: UIButtonType.custom)
    button.setTitle(title, for: .normal)
    button.frame = frame
    button.setTitleColor(UIColor.white, for: .normal)
    button.titleLabel?.font = UIFont(name: font, size: CGFloat(fontSize))
    button.addTarget(self, action: selector, for: .touchUpInside)
    self.view.addSubview(button)
}

These 4 buttons have text. When you click each of these, text on the buttons is not changing. The functions are using set title to update the text on the buttons, but for every run of the app the buttons do their function but the text on them remains at 0.

Upvotes: 0

Views: 1183

Answers (1)

Bubu
Bubu

Reputation: 669

Okay... so many codes. So basically, to change the button.text, use

button.setTitle("new text", for: .normal)

Also, when user clicks on the button,first thing you maybe want to disable it by:

button.isEnable = false

Then after click action, enable it again

button.isEnable = true

This is because it may cause unexpected error when user intentionally/unintentionally multi click on that button.

Upvotes: 2

Related Questions