2017-07-22 203 views
0

我在Swift中編寫了一個應用程序,其中所有元素都以編程方式添加。UIButton上的文本沒有更新SWIFT

//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) 
} 

這4個按鈕有文字。當您單擊其中每個按鈕時,按鈕上的文本不會更改。這些函數使用set title來更新按鈕上的文本,但是對於應用程序的每次運行,按鈕都起作用,但其上的文本保持爲0.

+0

請編輯您的問題以包括相關代碼(作爲文本,而不是鏈接或圖片)。 – rmaddy

+0

請只發布相關的代碼。將其縮小爲單個按鈕,並只顯示與您的問題相關的代碼。 – rmaddy

+0

你必須更精確和整潔。 –

回答

0

好吧...很多代碼。所以基本上,改變button.text,使用

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

此外,當用戶點擊該按鈕,首先你可能想將其禁用:

button.isEnable = false 

然後點擊之後的動作,使它再次

button.isEnable = true 

這是因爲它可能會導致意外的錯誤,當用戶有意/無意地多點擊該按鈕。