简体   繁体   中英

IOS Swift finishing a function when a button is pressed

I have a function that counts to 100 in about 10 secs. That func is called everytime a button is pressed and sent to a UILabel to be able to see it on the screen.

However anytime the button pressed before the counting is complete, the textview toggles between the first set 100 and the second set of 100.

I would like to know how can I program it so that it completes the function and shows 100 if the button is pressed while it is still running. Below is a snippet of code.

@IBAction func actionButton(sender: UIButton) {

for i in 1...100 {
        let timeToDelay = Double(i)
        delay(timeToDelay / 4) {

            label.text = i

        }
}

Thanks in advance

Try this:

@IBAction func actionButton(sender: UIButton) {
sender.enabled = false
for i in 1...100 {
        let timeToDelay = Double(i)
        delay(timeToDelay / 4) {

            label.text = i

        }
}
sender.enabled = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM