2

I am able to display the current time on my iPad application using the code,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
[dateFormatter setTimeStyle: NSDateFormatterShortStyle];


NSString *currentTime = [dateFormatter stringFromDate: [NSDate date]];
timeLabel.text = currentTime;

But this gives the time only when the application is loaded. How do i get the time to keep running? Like a digital clock.

Breakpoint
  • 1,511
  • 1
  • 18
  • 41

3 Answers3

10

Use this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
[dateFormatter setTimeStyle: NSDateFormatterShortStyle];

[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(targetMethod:)
userInfo:nil
repeats:YES]

Selector method would be like this:

-(void)targetMethod:(id)sender
{
  NSString *currentTime = [dateFormatter stringFromDate: [NSDate date]];
  timeLabel.text = currentTime;
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
6

Implement an NSTimer

How do I use NSTimer?

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

Then implement the targetMethod to check for and update your time!!!

If it were me, I would probably get the initial time, and only update my internal time using a 1 second timer.

You can go for higher timing resolution by implementing a faster timer (lets say 4 to 8 times faster), with this you will probably not get out of sync very often, but if you did, then you would be able to re-synchronize to the time returned by [NSData date]. In other words the faster your background task runs, the easier it would be for you to re-sync with the true time returned. This would also mean that you would only check for syncronization once out of every couple of times inside your target method.

Guess what Im saying is to remember Nyquist. Nyquist's theory (essentially) states that you should sample at least twice as fast as the resolution that you will ultimately try to reproduce using the dataset obtained from your sampling. In this case if you are trying to display to the user a once per second update, then you really should be sampling at no slower then 1/2 of a second to try and catch the transition from one second state to the next.

Community
  • 1
  • 1
trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • Rather than keeping a counter, simply display the current time whenever the timer ticks. – jscs Jun 13 '12 at 18:42
  • 1
    @Josh Caswell - Agreed, after looking up NSDate, that would be fine. All my years of having to deal with HW and being on a nats behind to be on the exact split second is where the nyquist stuff came in :-) – trumpetlicks Jun 13 '12 at 18:48
0

Note :- Declare in .h file

@property(nonatomic , weak) NSTimer *timer;
@property (weak, nonatomic) IBOutlet UIImageView *imgViewClock; //Image of Wall Clock
@property (weak, nonatomic) IBOutlet UIImageView *hourHandImgView; //Image of Hour hand
@property (weak, nonatomic) IBOutlet UIImageView *minuteHandImgView; //Image of Minute hand
@property (weak, nonatomic) IBOutlet UIImageView *secondHandImgView; //Image of Second hand

Note :- Declare in .m file

- (void)viewDidLoad {
[super viewDidLoad];
//Clock
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick) userInfo:nil repeats:YES];
[self tick];
}

//Here assign (tick) Method

-(void)tick {

NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *Components = [calendar components:units fromDate:[NSDate date]];
CGFloat hours = (Components.hour / 12.0) * M_PI * 2.0;
CGFloat mins = (Components.minute / 60.0) * M_PI * 2.0;
CGFloat seconds = (Components.second / 60.0) * M_PI * 2.0;

self.hourHandImgView.transform = CGAffineTransformMakeRotation(hours);
self.minuteHandImgView.transform = CGAffineTransformMakeRotation(mins);
self.secondHandImgView.transform = CGAffineTransformMakeRotation(seconds);

}
Anand Nanavaty
  • 554
  • 4
  • 13