在iOS中制作一个基本的计数器

时间:2015-08-20 04:46:34

标签: ios iphone swift counter

我在制作简单的计数器应用程序时遇到困难。我已经把代码放了但是当我点击“运行模拟”时,它总是给出错误信息。任何人都可以帮我找到我的错误吗?谢谢。

我的代码如下:

import UIKit

class ViewController: UIViewController {

    var counter: Int!
    @IBOutlet weak var Display: UILabel!

    func updateDisplay() {
        Display.text = String(counter)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        counter = 0
        updateDisplay()
        // 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 PlusButton(sender: AnyObject) {
        counter = counter + 1
        updateDisplay()
    }

    @IBAction func MinusButton(sender: AnyObject) {
        counter = counter - 1
        updateDisplay()
    }

    @IBAction func ClearButton(sender: AnyObject) {
        counter = 0
        updateDisplay()
    }
}

这是Xcode中的外观 https://www.facebook.com/photo.php?fbid=10204747519271053&set=pcb.10204747520111074&type=1&theater

这是我点击运行模拟时的错误消息 https://www.facebook.com/photo.php?fbid=10204747519351055&set=pcb.10204747520111074&type=1&theater

1 个答案:

答案 0 :(得分:0)

你的代码工作正常,但我认为你有layOut连接的问题,所以再次检查它应该是这样的:

Display标签:

enter image description here

PlusButton

enter image description here

MinusButton

enter image description here

对于ClearButton

enter image description here

检查THIS示例项目以获取更多信息。