可可中的计时器

时间:2011-11-07 23:35:30

标签: objective-c cocoa

例如,我有一个带有文本“text”的标签,我希望它在5秒内自动更改。如何在5秒内完成动作? 请提供代码示例。

1 个答案:

答案 0 :(得分:4)

你想要一个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"];
}