随着时间数据的变化,UILabel似乎没有更新

时间:2016-08-30 12:46:21

标签: ios swift

我花了几个月的时间学习swift,并且认为我已经开始制作多重计时器了,请原谅任何菜鸟错误!我创建了一个计时器类,它接收来自用户的输入数据并处理倒计时。这一切似乎都工作正常,但是当我点击开始按钮时,显示剩余时间的标签只会更新。 我的计时器类的代码如下:

class TimerClass: NSObject {

    var timer = NSTimer ()
    var startTimeCounter:NSTimeInterval = 0
    var hours = Int?()
    var minutes = Int?()
    var seconds = Int?()
    var timeString = String()
    var firstCheck:Bool = true

    func getTime() {
        // check if user has already clicked start so as to not double up the decrement
        if firstCheck == true {
            startTimeCounter += (NSTimeInterval(hours!) * 3600)
            startTimeCounter += (NSTimeInterval(minutes!) * 60)
            startTimeCounter += NSTimeInterval(seconds!)
            firstCheck = false
        }
        else {
            firstCheck = false
        }
        // turn the user data into hrs mins secs
        var timeRemaining : NSTimeInterval = startTimeCounter

        var hoursInt = Int(timeRemaining / 3600.0)
        timeRemaining -= (NSTimeInterval(hoursInt) * 3600)

        var minutesInt = Int(timeRemaining / 60.0)
        timeRemaining -= (NSTimeInterval(minutesInt) * 60)

        var secondsInt = Int(timeRemaining)
        timeRemaining -= NSTimeInterval(secondsInt)

        var strHours = String(format: "%02d", hoursInt)
        var strMinutes = String(format: "%02d", minutesInt)
        var strSeconds = String(format: "%02d", secondsInt)
        // create a string for the label
        timeString = "\(strHours):\(strMinutes):\(strSeconds)"
    }

    func updateTime () {
        startTimeCounter -= 1
        getTime()
    }

    func startTimer() {
        timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector:#selector(TimerClass.updateTime), userInfo: nil, repeats: true)

    }


}

我的视图控制器看起来像这样:

class ViewController: UIViewController {


    @IBOutlet weak var timeRemainingLabel: UILabel!
    @IBOutlet weak var startButton: UIButton!
    @IBOutlet weak var setDoneButton: UIButton!
    @IBOutlet weak var secStepper: UIStepper!
    @IBOutlet weak var minStepper: UIStepper!
    @IBOutlet weak var hourStepper: UIStepper!
    @IBOutlet weak var secLabel: UILabel!
    @IBOutlet weak var minLabel: UILabel!
    @IBOutlet weak var hourLabel: UILabel!
    @IBOutlet weak var setView: UIView!
    @IBOutlet weak var timerLabel: UILabel!
    @IBOutlet weak var setButton: UIButton!
    @IBOutlet weak var pauseButton: UIButton!


    var hourSelected = Int()
    var minSelected = Int()
    var secSelected = Int()
    var newTimer:TimerClass = TimerClass()
    var startPressed:Bool = false



    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.
    }

    // Set button pressed
    @IBAction func setPressed(sender: AnyObject) {
        self.setView.alpha = 0.8
    }

    // User closed set menu
    @IBAction func setDonePressed(sender: AnyObject) {
        self.setView.alpha = 0
        newTimer.hours = Int(self.hourStepper.value)
        newTimer.minutes = Int(self.minStepper.value)
        newTimer.seconds = Int(self.secStepper.value)
        newTimer.getTime()
    }

    // Stepper functions
    @IBAction func hourStepperPressed(sender: AnyObject) {
        self.hourLabel.text = String(format: "%.0f", hourStepper.value)
    }

    @IBAction func minStepperPressed(sender: AnyObject) {
        self.minLabel.text = String(format: "%.0f", minStepper.value)
    }

    @IBAction func secStepperPressed(sender: AnyObject) {
        self.secLabel.text = String(format: "%.0f", secStepper.value)
    }

    // User pressed start
    @IBAction func startPressed(sender: AnyObject) {
        self.newTimer.startTimer()
        updateTimerLabel()
        startPressed = true
    }

    func updateTimerLabel () {
        timeRemainingLabel.text = newTimer.timeString
    }
}

我很欣赏我可能已经解决了这个问题,但它是我的第一个独立应用程序,所以我仍然可以解决问题!

1 个答案:

答案 0 :(得分:1)

您只需在用户选择完成按钮时更新您的用户界面。

没有另一个计时器的一种方法是让你的TimerClass告诉你的视图控制器它需要更新UI。

您可以在TimerClass中创建协议,并将视图控制器设置为定时器'委托'对象

这样的事情:

protocol TimerDelegate: class {
    /**
    * Tell the delegate to update the interface
    **/
    func updateUI()
}

class TimerClass: NSObject {  
    var timer = NSTimer ()
    ...
    var firstCheck:Bool = true

    weak var delegate: TimerDelegate?

    func getTime() {
        ///
    }

    func updateTime () {
        startTimeCounter -= 1
        getTime()
        delegate?.updateUI()
    }

    func startTimer() {
        ...
    }
}

然后是你的视图控制器

class ViewController: UIViewController, TimerDelegate
{
    @IBOutlet weak var timeRemainingLabel: UILabel!
    ...
    @IBOutlet weak var pauseButton: UIButton!

    var hourSelected = Int()
    ...
    var startPressed:Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()
        newTimer.delegate = self
    }

    // Set button pressed
    @IBAction func setPressed(sender: AnyObject) {
        ...
    }

    // User closed set menu
    @IBAction func setDonePressed(sender: AnyObject) {
        ...
    }

    // Stepper functions
    @IBAction func hourStepperPressed(sender: AnyObject) {
        ...

    }
    @IBAction func minStepperPressed(sender: AnyObject) {
        ...
    }
    @IBAction func secStepperPressed(sender: AnyObject) {
        ...
    }

    //MARK: TimerDelegate

    func updateUI() {
        updateTimerLabel()
    }

    // User pressed start
    @IBAction func startPressed(sender: AnyObject) {
        self.newTimer.startTimer()
        updateTimerLabel()
        startPressed = true
    }

    func updateTimerLabel () {
        timeRemainingLabel.text = newTimer.timeString
    }
}