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.