单击按钮后更改label.text

时间:2017-04-05 13:51:46

标签: swift xcode

我希望制作一个数学测验应用程序。当程序加载时,标签被设置为随机创建的第一个问题,并且不使用数组来保存问题。

正确答案和“假答案”正确地分配给4个按钮,我现在要做的是当点击其中一个按钮时,我想检查按钮当前标题是否与正确相同回答然后继续并生成新的问题和答案。

这是我写的代码,我认为它会按照我的意愿行事,但事实并非如此。

 @IBAction func button1Press(_ sender: Any) {
    if button1.currentTitle == ("\(answer)")
    {
        rightAnswerCount = rightAnswerCount + 1
        generateQuestion()
        generateAnswer()
        displayButtonValues()
    }
    else {
        wrongAnswerCount = wrongAnswerCount + 1
        generateQuestion()
        generateAnswer()
        displayButtonValues()
    }
}

我可以对此代码进行哪些改进以使其正常工作?

谢谢!

从OP的评论中添加

要指定按钮标题,我这样做;

if buttonNo == 4 { 
    button4.setTitle("(answer)", for: .normal) 
} else { 
    button4.setTitle("(answer + 5)", for: .normal) 

}

其他功能;

func generateQuestion(){
    switch symbolNo {
    case 1:
        operatorSymbol = "+"
        self.lbl.text = "\(firstNo) + \(secondNo) = ?"
        question = "\(firstNo) + \(secondNo) = ?"
    case 2 :
        operatorSymbol = "-"
        self.lbl.text = "\(firstNo) - \(secondNo) = ?"
        question = "\(firstNo) - \(secondNo) = ?"
    case 3 :
        operatorSymbol = "/"
        self.lbl.text = "\(firstNo) / \(secondNo) = ?"
        question = "\(firstNo) / \(secondNo) = ?"
    case 4 :
        operatorSymbol = "x"
        self.lbl.text = "\(firstNo) x \(secondNo) = ?"
        question = "\(firstNo) x \(secondNo) = ?"

    default:
        self.lbl.text = ""
    }
}

func generateAnswer(){
    switch symbolNo {
    case 1:
        answer = firstNo + secondNo
    case 2:
        answer = firstNo - secondNo
    case 3:
        answer = firstNo / secondNo
    case 4:
        answer = firstNo * secondNo
    default:
        answer = 0
    }
}

func displayButtonValues(){
    if buttonNo == 1 {
        button1.setTitle("\(answer)", for: .normal)
    }
    else {
        button1.setTitle("\(answer - 4)", for: .normal)
        }
    if buttonNo == 2 {
        button2.setTitle("\(answer)", for: .normal)
    }
    else {
        button2.setTitle("\(answer - 2)", for: .normal)
        }
    if buttonNo == 3 {
        button3.setTitle("\(answer)", for: .normal)
    }
    else {
        button3.setTitle("\(answer + 3)", for: .normal)
        }
    if buttonNo == 4 {
        button4.setTitle("\(answer)", for: .normal)
    }
    else {
        button4.setTitle("\(answer + 5)", for: .normal)
        }

    }

我在这里也有这3个功能,这样当应用程序第一次加载时,这些功能会立即执行;

override func viewDidLoad() {
    super.viewDidLoad()
    generateQuestion()
    generateAnswer()
    displayButtonValues()

    // Do any additional setup after loading the view, typically from a nib.
}

变量

let firstNo = Int(arc4random_uniform(10)+1)
let secondNo = Int(arc4random_uniform(10)+1)
let symbolNo = Int(arc4random_uniform(4)+1)
let buttonNo = Int(arc4random_uniform(4)+1)
var operatorSymbol = ""
var answer = Int()
var question = ""
var rightAnswerCount = 0
var wrongAnswerCount = 0

2 个答案:

答案 0 :(得分:0)

也许你应该用这种方式比较你的按钮:

if button1.titleLabel?.text==answer{
    print("good answer")
    rightAnswerCount += 1
}else{
    print("bad answer")
    wrongAnswerCount += 1
}
generateQuestion()
generateAnswer()
displayButtonValues()

答案 1 :(得分:0)

问题在于:

let firstNo = Int(arc4random_uniform(10)+1)
let secondNo = Int(arc4random_uniform(10)+1)
let symbolNo = Int(arc4random_uniform(4)+1)
let buttonNo = Int(arc4random_uniform(4)+1)

在生成每个问题之前,需要将这些常量更改为var并指定新值。这解释了为什么它第一次工作,但之后没有看到任何变化。