Objective-C NSTimer 更新标签不更新

发布于 2024-10-16 10:23:26 字数 847 浏览 0 评论 0原文

我有以下两种方法。一种方法(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱她像谁 2024-10-23 10:23:26

假设持续时间是 NSTextField 的一个实例(谷歌搜索后,情况似乎如此 - 我找不到 NSLabel 是现有对象),请尝试这个;

[duration setStringValue:[NSString stringWithFormat:@"%d", trackDuration]];

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;

[duration setStringValue:[NSString stringWithFormat:@"%d", trackDuration]];
相思故 2024-10-23 10:23:26

您需要设置标签的文本:

duration.text = [NSString stringWithFormat:@"%d", trackDuration]];

You need to set the label's text:

duration.text = [NSString stringWithFormat:@"%d", trackDuration]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文