0

I wish to use it to change text on a button to that of another label after a certain period of time.

For example:

label1.text = "Stack Overflow"

//Wait 5 seconds

button1.setTitle( "\(label1.text!)", forState: UIControlState.Normal)
Dharmesh Kheni
  • 67,254
  • 32
  • 154
  • 160
Fiducial13
  • 983
  • 2
  • 8
  • 15

1 Answers1

2

I created this extension on Double:

extension Double {
    private func delay(delay:Double, closure:()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
    }
    // Delay function written by Matt on http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift/24318861#24318861



    func waitSecondsAndDo (closure:()->()) {
        delay (self, closure: closure)
    }
}

And then you can use it in your code like this:

5.0.waitSecondsAndDo {
   button1.setTitle( "(label1.text!)", forState: UIControlState.Normal)
}
Raymond
  • 3,510
  • 2
  • 17
  • 22