我希望用它在一段时间后将按钮上的文字更改为另一个标签的文字。
对于示例:
label1.text = "Stack Overflow"
//Wait 5 seconds
button1.setTitle( "\(label1.text!)", forState: UIControlState.Normal)
答案 0 :(得分:2)
我在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)
}
}
然后你可以在你的代码中使用它:
5.0.waitSecondsAndDo {
button1.setTitle( "(label1.text!)", forState: UIControlState.Normal)
}