Swift Button变灰了

时间:2016-06-12 18:06:45

标签: swift

我正在比较两个数字,如果两个数字不相等,计数器会增加1。 但是,标签不会更新,开始按钮会变灰。按钮显示为灰色,直到这两个数字相等。

    import UIKit

enum modes {
case start
case cancel
}

class ViewController: UIViewController {

    var mode: modes = modes.start
    var num1: Int = 0
    var num2: Int = 1
    var count: Int = 0
    @IBOutlet weak var start: UIBarButtonItem!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var countLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func startClicked(sender: AnyObject) {
        if (mode == modes.start) {
            mode = modes.cancel
            start.title = "cancel"
            checkNums()
        } else if (mode == modes.cancel) {
            mode = modes.start
            start.title = "start"
            num1 = 0
            num2 = 1
        }
    }

func checkNums() {
    while (num1 != num2) {
        let temp: UInt32 = 100000
        num1 = Int(arc4random_uniform(temp))
        num2 = Int(arc4random_uniform(temp))
        print("\(num1) and \(num2)")
        update()
    }

    func update() {
        count += 1
        label.text = "\(num1) and \(num2)"
        countLabel.text = "\(count)"
    }
}

请帮忙! 谢谢。

2 个答案:

答案 0 :(得分:0)

我无法在任何地方看到您更新num1和num2的值。不确定你到底想要实现什么,但我猜你应该在更新功能中为这两个数字设置新值?

答案 1 :(得分:0)

您可以使用UICountingLabel代替UILabel