随着ios时间的推移,我如何更新标签

时间:2013-10-02 13:44:37

标签: ios nsdate

  

如果在ios中发布帖子的时间过去,我如何保持UILabel的更新...如果>刚刚发布它应该说0并且随着时间的推移它变为> 1分钟,2分钟...... 1小时,2小时...... 1天等等   我是ios开发的新手   谢谢你的帮助!

3 个答案:

答案 0 :(得分:1)

如果我能正确理解您的问题,您需要使用NSTimer

答案 1 :(得分:1)

在iOS中,定期做一些事情很容易。

在您的主要代码中:

[NSTimer scheduledTimerWithTimeInterval: 1.0
                                 target: self
                               selector: @selector(doSomething:)
                               userInfo: nil
                                repeats: YES];

然后添加doSomething方法:

- (void)doSomething:(NSTimer *)timer {
    NSLog(@"We did it!");
}

答案 2 :(得分:1)

首先,您需要保留用户发布的日期。您可以使用以下方法轻松完成:

NSDate* postedDate = [NSDate now];

如果您想要找到您可以使用的时间之后的秒数:

NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:postedDate];

这为您提供了发布日期的秒数 - 您需要格式化输出。

为了保持标签更新本身,您应该使用NSTimer:

[NSTimer scheduledTimerWithTimeInterval: 1.0f
                                 target: self
                               selector: @selector(updateLabel:)
                               userInfo: nil
                                repeats: YES];

updateLabel将“timeDiff”格式化为人类可读的内容,如1分钟/天等......