Call NSTimer Method

I am new to working with timers on iPhoneand need some support.

I have a method as shown below that takes time and updates UILabelin mine userinterface. I also have NSTimerone that calls this method once per second (I only show hours and minutes). This works fine except for the first second when the application is in real time (since I understand that it takes one second before timercalling the method).

I would like to name my method from viewDidLoad, but how can I provide the correct arguments? Or is there a better way to do 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 points me in the right direction.

+5
2

? , [self updateTime:nil].

+2

... :

[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];
+2

All Articles