Objective-C NSTimer 更新标签不更新
我有以下两种方法。一种方法(timerHit
)获取当前播放歌曲的曲目持续时间,从中减去 1 并将其打印在日志中,然后也用该值更新标签。另一种方法 (countDown
) 每秒重复调用 timerHit
方法,但它似乎无法正常工作,NSLog
语句有效,并且重复,从而每秒打印该值,但更新标签语句 [duration setDoubleValue:trackDuration];
不起作用。谁能帮助我吗?
- (void)countDown {
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerHit:) userInfo:nil repeats:YES];
}
- (void)timerHit:(NSTimer *)p_timer
{
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
double trackDuration = [[iTunes currentTrack] duration];
if( trackDuration <= 1 && [p_timer isValid] )
[p_timer invalidate];
trackDuration--;
NSLog(@"%d", trackDuration);
[duration setDoubleValue:trackDuration];
}
谢谢,萨米。
I have two methods below. One method (timerHit
) which gets the track duration of the current playing song, minuses 1 from it and prints it out in the log it then also updates a label with the value too. The other method (countDown
) which repeats every second calls the timerHit
method, however it doesn't seem to work correctly the NSLog
statement works and repeats thus printing the value every second but the update label statement [duration setDoubleValue:trackDuration];
doesn't work. Can anyone help me?
- (void)countDown {
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerHit:) userInfo:nil repeats:YES];
}
- (void)timerHit:(NSTimer *)p_timer
{
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
double trackDuration = [[iTunes currentTrack] duration];
if( trackDuration <= 1 && [p_timer isValid] )
[p_timer invalidate];
trackDuration--;
NSLog(@"%d", trackDuration);
[duration setDoubleValue:trackDuration];
}
Thanks, Sami.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设持续时间是 NSTextField 的一个实例(谷歌搜索后,情况似乎如此 - 我找不到 NSLabel 是现有对象),请尝试这个;
Assuming that duration is an instance of NSTextField (After Googling, this seems to be the case - I can't find NSLabel to be an existing object), try this;
您需要设置标签的文本:
You need to set the label's text: