【问题标题】:Getting the current time and keeping it updated - Objective C获取当前时间并保持更新 - Objective C
【发布时间】:2013-03-17 10:14:30
【问题描述】:

我知道如何使用 NSDate 来获取时间并将其显示在 UILabel 中。

我需要显示日期 + 小时和分钟。 知道如何在不忙于等待的情况下保持更新吗?

谢谢!

【问题讨论】:

  • [currentDate description] 会给我当前时间,但我不想一直问...

标签: iphone ios objective-c


【解决方案1】:

使用 NSTimer 更新标签上的时间

- (void)viewDidLoad
 {
  [super viewDidLoad];

   [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
 }



-(void)updateTime
{


NSDate *date= [NSDate date];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init]; //for hour and minute

formatter1.dateFormat = @"hh:mm a";// use any format 

clockLabel.text = [formatter1 stringFromDate:date];

[formatter1 release];


}

【讨论】:

  • 正如我之前写的那样——这正是我试图避免的。我想也许听一些每次分钟变化时触发的通知?
  • 没有这样的通知。如果您需要定期发生某些事情(例如更改标签的文本),请使用计时器。
【解决方案2】:

正如您的 cmets 所说,如果您想在分钟更改时更改 label.text

你应该这样做:

1st:获取当前时间:

NSDate *date = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [calendar components:NSHourCalendarUnit fromDate:date];

并设置label.text = CURRENTHOUR_AND_YOURMINNUTS;

然后在下一分钟刷新标签,像这样:

第一个,你可以在60 - nowSeconds之后查看 [self performSelector:@selector(refreshLabel) withObject:nil afterDelay:(60 - dateComponents.minute)];

- (void)refreshLabel
{
    //refresh the label.text on the main thread
    dispatch_async(dispatch_get_main_queue(),^{      label.text = CURRENT_HOUR_AND_MINUTES;    });
    // check every 60s
    [self performSelector:@selector(refreshLabel) withObject:nil afterDelay:60];
}

它会每分钟检查一次,所以效果比上面的答案要多。

refreshLabel被调用时,表示分钟改变了

【讨论】:

  • performSelector:afterDelay: 并不比定时器更有效,而且这个解决方案是不必要的复杂化。
  • @JoshCaswell 你没有仔细看我的回答。他想每分钟刷新一次标签。所以我调用刷新标签,每 60 秒,他们每 1 秒刷新一次,所以我说我的回答更有效率
  • 我不明白这个区别,你是对的,但实际上最好以比你实际想要显示的时间更小的间隔检查时间,因为既不是 NSTimer 也不是 performSelector:afterDelay:保证在设定的时间发射。您也不需要主队列或NSDateComponents 的位,而且完全不清楚CURRENTHOUR_AND_YOURMINNUTS; 应该是什么。
  • @JoshCaswell。 NSDateComponents 是获取小时和分钟(通过dateComponents.hourdateComponents.minute ),因为他想显示小时和分钟,我不知道他想要什么格式所以我使用CURRENTHOUR_AND_YOURMINNUTS 。并且刷新 UI(label.text) 不能使用主线程?
  • 在此之前您不在后台线程中,您应该使用格式化程序进行格式化。
【解决方案3】:

您可以使用 NSTimer 定期获取当前时间。

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

- (void)timerFired:(NSTimer*)theTimer{
 //you can update the UILabel here.
}

【讨论】:

  • 这正是我试图避免的......我不能听一些每次分钟变化时触发的通知吗?
  • @user1369740 据我所知,没有这样的通知。
【解决方案4】:

您可以使用 NSTimer ,但是,鉴于上述方法,UILabel 不会在触摸事件上更新,因为主线程将忙于跟踪它。您需要将其添加到 mainRunLOOP

    NSTimer* timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(updateLabelWithDate) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

-(void)updateLabelWithDate
{
   //Update your Label
}

您可以更改时间间隔(您希望更新的速率)。

【讨论】:

  • 当使用scheduledTimer... 创建计时器时,它已经添加到运行循环中。这是不必要的。
猜你喜欢
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 2011-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
相关资源
最近更新 更多