ios - use an array to pass values into a TIMER IN SWIFT -
i have working countdown can increase pushing add button , thats time counts down (so user can set countdown)
i display starting time 00:00 in label.
when click button increase countdown begins @ 1 because @ moment int
can create array , increase individual indexes, display them within label?
can or point me in write code direction have below thanks
var timearray: [double : double] = [00, 00] var timer = nstimer() var countdown = 0 func runtimer() { timer = nstimer.scheduledtimerwithtimeinterval(1, target: self, selector:selector("updatetimer"), userinfo: nil, repeats: true) } func updatetimer() { if countdown > 0 { countdown-- timerlabel.text = string(countdown) } else { countdown = 0 timerlabel.text = string(countdown) } } @iboutlet weak var timerlabel: uilabel! @ibaction func increasecountdown(sender: anyobject) { countdown++ timerlabel.text = string(countdown) } @ibaction func startcountdown(sender: anyobject) { runtimer() } @ibaction func stopcountdown(sender: anyobject) { timer.invalidate() } override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
your syntax timearray
creating dictionary
, not array
. suggest use int
instead of double
:
var timearray:[int] = [0, 0]
to create label text field:
// format create string 2 digits minute , second // leading 0 each if needed "00:00" timerlabel.text = string(format: "%02d:%02d", timearray[0], timearray[1])
when create timer, convert time array simple countdown:
let countdown = timearray[0] * 60 + timearray[1]
an entirely different approach work store time integer seconds internally , display minutes , seconds.
let countdown = 100 // 1 minute, 40 seconds timerlabel.text = string(format: "%02d:%02d", countdown/60, countdown%60)
Comments
Post a Comment