Xcode (Swift) use a variable from another function -
i have timer starts when press button.
a random number generated , time should stop when reaches random number (from 1-100).
the way can work generating random number in time function (counting), generates random number every time counter increases, means stop if random number generates same counter. if random number , time not fall on same number, time goes above 100 - found putting in label show random number.
i have managed random number generate once putting in button function, timer function not recognise stopnumber. way seems practical, how call variable stopnumber button function in counting function?
code below:
func generatenewnumber() -> uint32 { return arc4random_uniform(100) + 1 } func counting() { var stopnumber:int = int(generatenewnumber()) timercount += 1 timerlabel.text = "\(timercount)" if timercount == stopnumber { timer.invalidate() timerrunning = false } @ibaction func startbutton(sender: uibutton) { if timerrunning == false && timercount == 0 { timer = nstimer.scheduledtimerwithtimeinterval(0.06, target: self, selector: selector("counting"), userinfo: nil, repeats: true) timerrunning = true } }
i new appreciated.
declare variable outside of function's scope insides class' scope. following code snippet shows concept of this. if need implementing code, try harder :) if still not work, ask again.
class myclass{ private var stopnumber:int = 15 func counting() { stopnumber++ //you can access variable within functions inside of myclass } }
Comments
Post a Comment