Custom Countdown Timer (Need Help Please)

Hey Everyone,
I’m an “Old Guy” and Fresh into Coding, trying to create my own App and would greatly appreciate some help (working with X-Code/Swift) and have worked through the CodeWithChris Tutorials…

  1. I created a COUNTDOWN TIMER (Label/Play/Pause/Reset) and it all works fine.

  2. My first version is based on a Manual Value (var counter = 120) and the 2 min counters runs okay.

  3. I added a PICKERVIEW with Set Times (10mins, 15mins, Etc) and have successfully linked my Pickerview to the Label.

HERE IS WHERE I AM STUCK!!

Although I can scroll to the preferred Time for the Clock, as soon as I START it reverts back to the 120 Seconds set, AND I CANNOT FIGURE out How To Link the “Counter = ??” to read the Pickerview.

var timer = Timer()
var isTimerRunning = false
var counter = 120 (I think this is the missing link)

Could you please post all of your code so we can get a better picture of what you’re talking about

Hey @MikaelaCaron,
My full CODE below, don’t laugh, it’s my first (lol)

import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate
{
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var playButton: UIButton!
@IBOutlet weak var pauseButton: UIButton!
@IBOutlet weak var resetButton: UIButton!
@IBOutlet weak var pickerView: UIPickerView!

let gameClock = ["07:00", "10:00", "15:00", "20:00", "25:00", "30:00", "35:00", "40:00"]

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return gameClock[row]
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return gameClock.count
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    timerLabel.text = gameClock[row]
}

var timer = Timer()
var isTimerRunning = false
var counter = 420

override func viewDidLoad() {
    super.viewDidLoad()
    
    resetButton.isEnabled = false
    pauseButton.isEnabled = false
    playButton.isEnabled = true
    }

@IBAction func playDidTap(_ sender: UIButton) {
    if !isTimerRunning {
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(runTimer), userInfo: nil, repeats: true)
        
        isTimerRunning = true
        resetButton.isEnabled = true
        pauseButton.isEnabled = true
        playButton.isEnabled = false
    }
}

@objc func runTimer() {
    counter -= 1

    let minute = counter / 60
    
    let second = (counter % 60) % 60
    var secondString = "\(second)"
    if second < 10 {
        secondString = "0\(second)"
    }
            timerLabel.text = "\(minute):\(secondString)"
    
}

@IBAction func pauseDidTap(_ sender: UIButton) {
    resetButton.isEnabled = true
    playButton.isEnabled = true
    pauseButton.isEnabled = false
    
    isTimerRunning = false
    timer.invalidate()
}

@IBAction func resetDidTap(_ sender: UIButton) {
    timer.invalidate()
    isTimerRunning = false
    counter = 0
    
    timerLabel.text = "07:00"
    resetButton.isEnabled = false
    playButton.isEnabled = true
}

}

// JDF: END OF GAME CLOCK (Countdown Timer)

No laughing! :slight_smile: we all had to start somewhere!

You never set counter equal to another value.

Inside your didSelectRow function you need to set counter = …
You could use an if statement something like

If gameClock[row] == “07:00”
{
counter = …
}

Sorry @MikaelaCaron, you lost me here (lol)
So that linw should read…"

func pickerView(_ pickerView: UIPickerView, didSelectRow row: If gameClock[row] == “07:00”, inComponent component: Int) {
timerLabel.text = gameClock[row]

If correct, do I list all the times there…?
And do I then remove the “var counter = 420”…?

@KoosNZ nooo sorry the if statement should go inside that function didSelectRow

like after your timerLabel.text=gameClock[row]

you can keep the counter = 420 as a default time, and you can change it with the pickerView

@MikaelaCaron, I’m sorry to waste your time but I do not understand :pensive: and cannot fit that anywhere without an error…

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
timerLabel.text = gameClock[row]; if gameClock[row] == “07:00” ERROR

Possible that you can just show me below please
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
timerLabel.text = gameClock[row]

@KoosNZ no problem! We all have trouble from time to time :slight_smile:

Try this inside your didSelectRow function. You’ll have to create a if statement for each element in your array gameClock, I just did the first 3

@MikaelaCaron You are a #Legend, THANKS :slight_smile:

@KoosNZ no problem! Let me know if that works!

Works PERFECTLY :+1::+1:

1 Like

@MikaelaCaron How can I reference or Credit you for the assist…?

1 Like

I marked my comment as solved! So when other people see it they’ll know that was the solution!

1 Like