I am trying to do the following:
1) user clicks button start, it runs startTimer method, which sets a new [NSDate date].
2) user clicks button stop it runs stopTimer method, which retrieves the [NSDate date] value.
I can’t get step number 2 working. I’ve set it in the .h file. If I copy the code from the start method into the stop method it works. So, I can set the [NSDate date]. But, this is not what I want. I want to be able to set it in the startTimer method. How do i go about this?
.h file
@interface StartTest : UIViewController {
IBOutlet UILabel *timer;
NSDate *startNSDate;
NSDate *start;
}
- (IBAction)startTimer;
- (IBAction)stopTimer;
- (NSDate *)setStart;
- (NSDate *)getStart;
@end
.m file:
@implementation Ash
- (IBAction)startTimer {
startNSDate = [NSDate date];
}
- (IBAction)stopTimer{
start = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
NSString *stringFromDate = [formatter stringFromDate:startNSDate]; // <<< this is where it fails
NSLog(@"stringfromdate: %@", stringFromDate);
}
startNSDate isn’t retained and by the time you want to access it, it’s already dealloc’d, hence you’re trying to access garbage pointer
easiest solution for you will be