Reputation: 3463
I have been playing around with some code from the developers library but struggling to understand what is going on with it. I'm trying to update a label with what I'm seeing printed in the NSLog though all I seem to be able to do is update the label with the formatted time when the button is pressed. I'd like it to update the label with what I can see being printed in the NSLog. I know there is probably a simpler way but I'm just trying to understand this code and how to update the UILabel with what's being printed in the NSLog? Thank you if you can help.
Here's my code:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak) NSTimer *repeatingTimer;
@property NSUInteger timerCount;
@property NSString *stringFromDate;
@property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)startRepeatingTimer:sender;
- (IBAction)buttonPressed:(id)sender;
- (void)targetMethod:(NSTimer*)theTimer;
- (NSDictionary *)userInfo;
@end
Here's the implementation:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
NSString *stringFromDate;
NSDateFormatter *formatter;
}
- (void)viewDidLoad
{
[super viewDidLoad];
formatter = [[NSDateFormatter alloc]init] ;
}
/************************************************/
/* Timer methods */
- (NSDictionary *)userInfo {
return @{ @"StartDate" : [NSDate date] };
}
- (void)targetMethod:(NSTimer*)theTimer {
NSDate *startDate = [[theTimer userInfo] objectForKey:@"StartDate"];
NSLog(@"Timer started on %@", startDate);
stringFromDate = [formatter stringFromDate:startDate];
[formatter setDateFormat:@"hh:mm:ss"];
self.label.text = [NSString stringWithFormat:@"%@",stringFromDate];
}
- (IBAction)startRepeatingTimer:sender {
// Cancel a preexisting timer.
[self.repeatingTimer invalidate];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self selector:@selector(targetMethod:)
userInfo:[self userInfo] repeats:YES];
self.repeatingTimer = timer;
}
- (IBAction)buttonPressed:(id)sender
{
[self startRepeatingTimer:sender];
}
@end
Upvotes: 0
Views: 114
Reputation: 318955
Since the problem is that you get the same date/time over and over, you need to change how you get the current date/time. Get rid of the use of the userInfo
on the timer. This is the cause of the problem.
- (void)targetMethod:(NSTimer*)theTimer {
NSDate *date = [NSDate date];
NSLog(@"Current date: %@", date);
[formatter setDateFormat:@"hh:mm:ss"];
stringFromDate = [formatter stringFromDate:date];
self.label.text = stringFromDate;
}
- (IBAction)startRepeatingTimer:sender {
// Cancel a preexisting timer.
[self.repeatingTimer invalidate];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5
target:self selector:@selector(targetMethod:)
userInfo:nil
repeats:YES];
self.repeatingTimer = timer;
}
Also, don't needlessly use stringWithFormat:
. And be sure to set the formatter's format before formatting the date.
Any why have the timer run every half second in your label only shows the time to the nearest second?
Upvotes: 1