开发者

Get the time to "run" in iOS

I've read a guide about how to make a clock, in iOS. And that works like it should and all that, but i want to go further and get the clock to update each minut. So it's no longer "static", so if you open the application 12:16 am, and let it run it should automatically update each minut(12:17 am, 12:18am etc.).

This is the code the guide provided me with,

NSDateFormatter *datoVar = [[NSDateFormatter alloc] init];
[datoVar setDateFormat:@"hh:mm"];

LabelKlokken.text = [datoVar stringFromDate:[NSDate date]];

I've tried and tried to look through google and stackoverflow, but just can't find anything.

Thanks in advance, Oluf Nielsen.

EDITED : My code now looks like this

-(void)clock
{
    NSDateFormatter *datoVar = [[NSDateFormatter alloc] init];
    [datoVar setDateFormat:@"hh:mm"];

    LabelKlokken.text = [datoVar stringFromDate:[NSDate date]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self clock]开发者_Python百科;
    tid = nil;
    tid = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(clock) userInfo:NULL repeats:YES];
}


You can schedule an NSTimer to check for time every 60 seconds.

dateTimer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];

put your code in updateTime method. Better yet, check it every second, because it could happen that you switch time in 59th second of each minute thus being 59 seconds behind... Remember to invalidate the timer once you want to quit updating so your app doesn't crash when you switch views or something.

[dateTimer invalidate]; dateTimer = nil;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜