One button, 2 action and name change, Swift? -
i'm new field, maybe simple question, not know, swift. know how make 2 buttons used stopwatch, start , stop. have same button start , stop stopwatch, , text change stop start, possible , how?
i able create simple ios project works requirements. here step step.
create uiviewcontroller
scene in project's storyboard
, name "viewcontroller". add uibutton
auto layout constraints in scene. in project navigator, create uiviewcontroller
subclass file following code in it:
import uikit class viewcontroller: uiviewcontroller { @iboutlet weak var mybutton: uibutton! var timer: nstimer? var myint = 0 var launchbool: bool = false { didset { if launchbool == true { mybutton.settitle("stop", forstate: .normal) timer = nstimer.scheduledtimerwithtimeinterval(1, target: self, selector: "firestopwatch", userinfo: nil, repeats: true) } else { mybutton.settitle("start", forstate: .normal) timer?.invalidate() timer = nil myint = 0 } } } @ibaction func buttonaction(sender: anyobject) { launchbool = !launchbool //true false, false true... } func firestopwatch() { myint += 1 println(myint) } override func viewdidload() { super.viewdidload() mybutton.settitle("start", forstate: .normal) } override func didreceivememorywarning() { super.didreceivememorywarning() } }
link @ibaction
, @iboutlet
uibutton
in viewcontroller scene. launch project.
each time click on uibutton
, toggles title between "start" , "stop". each time click on "start", displays increasing value of myint
in xcode console. each time click on "stop", stops nstimer
, resets myint
0.
Comments
Post a Comment