【问题标题】:Accessing current device time in my app在我的应用程序中访问当前设备时间
【发布时间】:2012-10-19 04:58:03
【问题描述】:

我想在我的应用程序中获取当前设备时间,并在我的设备时间更新时不断更新该时间。我遵循该代码

     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"dd/mm/yy hh:mm"];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSString*  todayString = [dateFormatter stringFromDate:[NSDate date]];

我得到了当前设备时间,但是当用户使用我的应用程序时它不会自动更新。 我该怎么办。

【问题讨论】:

  • 全大写标题在 StackOverflow 上不太受欢迎。

标签: ios4 nsdateformatter iphone-4 nstimezone


【解决方案1】:

todayString 必须随着设备时间的变化而变化。这意味着,您需要创建一个 60 秒的计时器,并且每 60 秒更新一次时间。

你需要这样做:

NSTimer *timer = [NSTimer timerWithTimeInterval:60.0 target:self selector:@selector(updateTimer:)
                                                   userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

- (void) updateTimer:(id) object
{
  // Write required code here.
}

【讨论】:

  • @Shashi Sharma:你的问题用我上面的解决方案解决了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 2017-11-05
  • 2018-04-29
  • 2015-04-29
  • 2023-02-05
  • 2012-07-24
  • 2018-06-26
相关资源
最近更新 更多