0

I tried following this guide that used string value but it wasn't that helpful. I'm working on an app that has 8 buttons, each will be assigned with a number representing seconds converted to minutes in CountdownVC. I'm trying to pass the Int value when I tap a button and assign the value to var seconds = 0 in CountdownVC.

When pressing the button in the MainVC:

@IBAction func oneMinuteBtnPressed(_ sender: Any) {

        let vc = CountdownVC()
        vc.seconds = 60
        performSegue(withIdentifier: "toCountdownVC", sender: self)
    }

I want to pass the assigned value to the CountdownVC's making it var seconds = 60:

import UIKit

class CountdownVC: UIViewController {

@IBOutlet weak var countdownLbl: UILabel!
@IBOutlet weak var startBtn: UIButton!
@IBOutlet weak var pauseBtn: UIButton!
@IBOutlet weak var resetBtn: UIButton!

// Variables
var seconds = 0 // This variable will hold the starting value of seconds. It could be any amount over 0
var timer = Timer()
var isTimerRunning = false // This will be used to make sure the only one time is created at a time.
var resumeTapped = false

override func viewDidLoad() {
    super.viewDidLoad()
    pauseBtn.isEnabled = false

}
func runTimer() {
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(CountdownVC.updateTimer)), userInfo: nil, repeats: true)
    isTimerRunning = true
    pauseBtn.isEnabled = true
}
@objc func updateTimer(){
    if seconds < 1 {
        timer.invalidate()
        //TODO: Send alert to indicate "time is up!"
    } else {
        seconds -= 1 //This will decrement(count down) the seconds.
        countdownLbl.text = timeString(time: TimeInterval(seconds)) //This will update the label
    }
}
func timeString(time: TimeInterval) -> String {

    let hours = Int(time) / 3600
    let minutes = Int(time) / 60 % 60
    let seconds = Int(time) % 60
    return String(format: "%02i:%02i:%02i", hours, minutes, seconds)
}

What I have above doesn't seem to work except for the segue part. Any help would be appreciated.

Joe Vargas
  • 65
  • 7

1 Answers1

0

As Quoc Nguyen pointed it out, you are assigning a value to a controller that will never be used.

When you call performSegue, this will instantiate a different vc from the one you have created.

If you want to pass something to the vc that is created by your storyboard you need to use something like this:

@IBAction func oneMinuteBtnPressed(_ sender: Any) {
        performSegue(withIdentifier: "toCountdownVC", sender: self)
}

 // This function is called before the segue
 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // get a reference to the your view controller
     if let nextViewController = segue.destination as? CountdownVC {
        nextViewController.seconds = 60
     }
 }
RomOne
  • 2,065
  • 17
  • 29
  • RomOne, Quoc and Suragch, thank you so much for your help. Your combined efforts helped me understand my issue a bit better. – Joe Vargas Apr 16 '18 at 14:10
  • No worries ;) Glad it helped. Don't forget to upvote or accept the right answer, so that other people can quickly see what was helpful for a given question. – RomOne Apr 17 '18 at 00:03