2011-11-07 16 views
1

例如,我有一個帶有文本「文本」的標籤,我希望它在5秒內自動更改。我如何在5秒內做出行動? 請給出一個代碼示例。Cocoa中的計時器

回答

3

你想要一個NSTimer。檢查出the documentation。您可能需要scheduledTimerWithTimeInterval:invocation:repeats:scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:。這裏有一個簡單的例子:

[NSTimer scheduledTimerWithTimeInterval:5 
           target:self 
           selector:@selector(updateString) 
           userInfo:nil 
           repeats:NO]; 

updateString是一樣的東西:

-(void)updateString 
{ 
    [textField setStringValue:@"new text"]; 
} 
+1

此外,發送定時'-invalidate'當你如果'重複完成了它:'是'YES'。 – Justin

+0

非常感謝! –