在倒计时结束时重置或更改按钮标题

Reset or change button title at the end of countdown

我正在为 iPhone/iPad 构建应用程序。我的应用程序有一个倒数计时器。倒计时开始按钮标记为"Start",按下按钮时切换为"Running"。当倒计时到达 00:00 时,我希望按钮标题将自身重置为 "Start" 或将其更改为 "Restart",以便用户可以重新开始。

我是 Swift 的新手,所以我希望有人能对此提供帮助。这是我的代码:

import Foundation
import UIKit
import AVFoundation


class VC11 : UIViewController {


    @IBOutlet weak var timerLabel: UILabel!



    var timer = NSTimer()
    var count = 240
    var timerRunning = false
    var audioPlayer = AVAudioPlayer()


    override func viewDidLoad() {
        super.viewDidLoad()


            func nextPage(sender:UISwipeGestureRecognizer) {
                switch sender.direction {

                case UISwipeGestureRecognizerDirection.Left:
                    print("SWIPED LEFT")
                    self.performSegueWithIdentifier("seg11", sender: nil)
                default:
                    break

                }


                var leftSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))
                var rightSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))

                leftSwipe.direction = .Left
                rightSwipe.direction = .Right

                view.addGestureRecognizer(leftSwipe)
                view.addGestureRecognizer(rightSwipe)
            }


        }



    func updateTime() {
        count--

            let seconds = count % 60
            let minutes = (count / 60) % 60
            let hours = count / 3600
            let strHours = hours > 9 ? String(hours) : "0" + String(hours)
            let strMinutes = minutes > 9 ? String(minutes) : "0" + String(minutes)
            let strSeconds = seconds > 9 ? String(seconds) : "0" + String(seconds)
            if hours > 0 {
                timerLabel.text = "\(strHours):\(strMinutes):\(strSeconds)"
            }
            else {
                timerLabel.text = "\(strMinutes):\(strSeconds)"

        }
        stopTimer()


           }
  @IBAction func startTimer(sender: AnyObject) {
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)

    sender.setTitle("Running...", forState: .Normal)

       }


    func stopTimer()

    {

        if count == 0 {
            timer.invalidate()
            timerRunning = false
            timerLabel.text = "04:00"
            playSound()
            count = 240

        }


    }




    func playSound() {

    var soundPath = NSBundle.mainBundle().pathForResource("Metal_Gong", ofType: "wav")
    var soundURL = NSURL.fileURLWithPath(soundPath!)


        self.audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil)
        self.audioPlayer.play()
    }


}

很简单,如果你在 storyBoard 中有按钮,那么只需从 storyBoard 中取出按钮,就像你为 timerlabel 取的一样,就像这样

@IBOutlet weak var yourButton: UIButton!

然后在您的 stopTimer() 函数中,像这样更改按钮的标题,

 func stopTimer()

{

    if count == 0 {
        timer.invalidate()
        timerRunning = false
        timerLabel.text = "04:00"
        playSound()
        count = 240
        yourButton.setTitle("Restart", forState: .Normal)// add this line in your code
    }
}