I have a timer which starts when you press a button.
A random number is generated and the time should stop when it reaches the random number (from 1-100).
The only way I can get it work is by generating the random number in the time function (counting), however this generates a random number every time the counter increases, which means it will only stop if the random number generates the same as the counter. If the random number and the time do not fall on the same number, the time goes above 100 - I found this by putting in a label to show the random number.
I have managed to get the random number to generate just once by putting it in the button function, however then the timer function does not recognise stopNumber. this way seems to be the most practical, how do I call the Variable stopNumber from the button function in the 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 am fairly new to this any help would be really appreciated.
startButton, pass the value as an argument tocounting?