Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 16th, 2012  |  syntax: None  |  size: 2.67 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. NSTimer not working on StopWatch App ONLY ON SIMULATOR (Shows static strange number)
  2. #import <UIKit/UIKit.h>
  3.  
  4. @interface ViewController : UIViewController
  5. {
  6.     NSTimer *stopWatchTimer;
  7.     NSDate *startDate;
  8. }
  9.  
  10.  
  11. @property (strong, nonatomic) IBOutlet UILabel *stopWatchLabel;
  12.  
  13. - (IBAction)startButtonTapped:(id)sender;
  14.  
  15. - (IBAction)stopButtonTapped:(id)sender;
  16.  
  17. -(void)updateTimer;
  18.  
  19. @end
  20.        
  21. - (IBAction)startButtonTapped:(id)sender {
  22.  
  23.     stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
  24.                                                       target:self
  25.                                                     selector:@selector(updateTimer)
  26.                                                     userInfo:nil
  27.                                                      repeats:YES];
  28. }
  29.  
  30.  
  31. - (void)updateTimer
  32. {
  33.     NSDate *currentDate = [NSDate date];
  34.     NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
  35.     NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
  36.  
  37.     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  38.     [dateFormatter setDateFormat:@"HH:mm"];
  39.     [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
  40.  
  41.     NSString *timeString=[dateFormatter stringFromDate:timerDate];
  42.     stopWatchLabel.text = timeString;
  43. }
  44.  
  45.  
  46. - (IBAction)stopButtonTapped:(id)sender {
  47.  
  48.     [stopWatchTimer invalidate];
  49. }
  50.        
  51. - (IBAction)startButtonTapped:(id)sender {
  52.     startDate = [NSDate date];
  53.  
  54.     stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
  55.                                                       target:self
  56.                                                     selector:@selector(updateTimer)
  57.                                                     userInfo:nil
  58.                                                      repeats:YES];
  59. }
  60.        
  61. - (void)updateTimer
  62. {
  63.     NSDate *currentDate = [NSDate date];
  64.     NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
  65.  
  66.     NSInteger minutes = floor(timeInterval/60);
  67.     NSInteger seconds = trunc(timeInterval - minutes * 60);
  68.  
  69.     NSString *timeString=[NSString stringWithFormat:@"%i:%02i", minutes, seconds];
  70.     stopWatchLabel.text = timeString;
  71. }
  72.        
  73. - (void)updateTimer
  74. {
  75.     NSDate *currentDate = [NSDate date];
  76.     NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  77.     NSDateComponents *components = [calendar components:NSSecondCalendarUnit|NSMinuteCalendarUnit fromDate:startDate toDate:currentDate options:0];
  78.     NSInteger minutes = [components minute];
  79.     NSInteger seconds = [components second];
  80.  
  81.     NSString *timeString=[NSString stringWithFormat:@"%i:%02i", minutes, seconds];
  82.     stopWatchLabel.text = timeString;
  83.  }