swift - Want to understand a relation -
i beginner learning swift 3 in xcode 8 , building basic app called "eggtimer". code written below , don't understand how timerlabel.text
linked timer didn't set connection between them.
next star //*
can write } else { timer.invalidate()
, labeltimer.text
nicely stops decreasing, how can happen? selector
in timer
properties mean? sorry english , answers.
class viewcontroller: uiviewcontroller { var timer = timer() var time = 210 func decreasetimer() { if time > 0 { time -= 1 timerlabel.text = string(time) } else { //* timerlabel.text = string(time) } } @iboutlet var timerlabel: uilabel! @ibaction func timerstarter(_ sender: anyobject) { timer = timer.scheduledtimer(timeinterval: 1, target: self, selector: #selector(viewcontroller.processtimer), userinfo: nil, repeats: true) } }
lets start bottom: selector
specifies method should called every 1 second (timeinterval
parameter). in case should changed following:
timer = timer.scheduledtimer(timeinterval: 1, target: self, selector: #selector(viewcontroller.decreasetimer), userinfo: nil, repeats: true)
as can see selector
called decreasetimer
, method specified in top.
the timer calls method every time updates. in method decrease time
var , update text of timerlabel
.
timer.invalidate()
stopps timer
when time
reaches 0.
i hope clarifies questions.
Comments
Post a Comment