开发者

Calling a NSTimer method

I'm new to working with timers on the iPhone and would need some support.

I have a method as below that takes the time and update a UILabel in my userinterface. I also have an NSTimer that calls that method once a second (I only show hours and minutes). This works just fine, except for the first second the App is live (as I understand it takes one second before the timer calls the method).

I would like to call my method from viewDidLoad, but how can I supply the right arguments? Or is there a better way of doing this?

// Timer for updating time
[NSTimer scheduledTimerWithTimeInterval:1.0f 
                                 target:self 
                               selector开发者_高级运维:@selector(updateTime:) 
                               userInfo:nil 
                                repeats:YES];   


-(void)updateTime: (NSTimer *) timer {

    currentTime = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
    [dateFormatter release];

    timeLabel.text =  currentTimeString;
}

Appreciate anything that would point me in the right direction.


In your method, you don't use the timer variable right? So, why just call [self updateTime:nil]. That should work


One more option ... you can do:

[NSTimer scheduledTimerWithTimeInterval:0.01f 
                                 target:self 
                               selector:@selector(updateTime:) 
                               userInfo:nil  
                                repeats:NO];    

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜