ios - 简单的快速秒表不起作用

标签 ios iphone swift stopwatch

这是我的代码。

//
    //  ViewController.swift
    //  Stopwatch
    //
    //  Created by Orkun Atasoy on 12.09.15.
    //  Copyright (c) 2015 Orkun Atasoy. All rights reserved.
    //


import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var timerLabel: UILabel!
    @IBOutlet weak var timerButton: UIButton!

    var timer : NSTimer?
    var ms = 0

    @IBAction func buttonTapped(sender: AnyObject) {

        timerButton.setTitle("Stopp", forState:UIControlState.Normal)

        self.timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
    }


    func update() {
        self.ms++
        timerLabel.text = String(self.ms)enter code here
    }



}

问题是当我运行构建时,它出现了带有大文本“秒表”的介绍屏幕,它就像卡住在那里一样。但它应该带有一个带有按钮下方的标签,上面有一个文本“开始”。当我单击按钮时,它应该开始计数并且标签应该更改为“stopp”。当我再次单击时,它应该会停止计时器。

我不明白问题是什么。我是 Swift 的新手。如果你能帮助我,我会很高兴。

感谢您的关注

编辑 >> 标签文本以“00:00”开头。

最佳答案

看起来你在搞乱 IBOutlet 连接,我已经更正了你的一些代码,这里是工作代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var timerLabel: UILabel!
    @IBOutlet weak var timerButton: UIButton!

    var timer : NSTimer?
    var ms = 0

    override func viewDidLoad() {

        timerLabel.text = "00:00"
    }

    @IBAction func buttonTapped(sender: AnyObject) {

        if timerButton.currentTitle == "Stopp" {
            timer?.invalidate()
            timerButton.setTitle("Start", forState:UIControlState.Normal)
        } else {

            timerButton.setTitle("Stopp", forState:UIControlState.Normal)

            self.timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
        }
    }

    func update() {
        self.ms++
        timerLabel.text = String(self.ms)

    }
}

首先从 Storyboard View Controller 中删除所有 socket 并以这种方式连接它:

enter image description here

如果你想检查 THIS示例项目。

关于ios - 简单的快速秒表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32538608/

相关文章:

ios - 更新现有实体时核心数据模型配置不兼容

ios - 是否可以在应用程序加载数据并卡住时显示警报?

iOS:逐行添加/更新 UITableView 单元格

ios - 如何根据其内容调整 UITextView 的大小?

ios - UIImagePickerController 和外部显示

iphone - 在 iPhone 应用程序中使用单选按钮

iphone - 如何检测 CATiledLayer 何时完成绘制所有图 block

ios - 当数据模型实体超过3个时,Xcode构建会挂起

ios - 使用 tableViewController 时 iphone x home 指示器问题

ios - 使用 GPUImagePicture 和自定义 LUT 进行 GPUImageVideoCamera 实时预览