计时器IBOutlet消失。可能很简单

时间:2013-09-01 09:13:47

标签: ios timer

当我运行此代码时,datePicker选择与当前实时之间的差异通过IBOutlet以“dd:hh:mm:ss”的形式显示。我已经设置了一个IBAction按钮,从IBOutlet标签中显示的时间开始计时器倒计时,但当我点击按钮启动计时器时,显示计时器的标签消失。谢谢!

@synthesize picker;     @synthesize标签;

-(IBAction)startTimer:(id)sender{
    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(refreshLabel)
                                   userInfo:nil
                                    repeats:YES];
}

-(IBAction)displayDate:(id)sender {
        NSDate *todaysDate = [NSDate date];
        NSDate *selected = [picker date];

        NSCalendar *gregorian = [[NSCalendar alloc]
                                 initWithCalendarIdentifier:NSGregorianCalendar];

        NSUInteger unitFlags = NSDayCalendarUnit |NSMinuteCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit;
        NSDateComponents *dateComparisonComponents = [gregorian components:unitFlags
                                                                  fromDate:todaysDate
                                                                    toDate:selected
                                                                   options:NSWrapCalendarComponents];

        NSInteger days = [dateComparisonComponents day];
        NSInteger hours = [dateComparisonComponents hour];
        NSInteger minutes = [dateComparisonComponents minute];
        NSInteger seconds = [dateComparisonComponents second];

        self.label.text = [NSString stringWithFormat:@"%ld:%02ld:%02ld:%02ld",
                           (long)days,
                           (long)hours,
                           (long)minutes,
                           (long)seconds];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSDate *now = [NSDate date];
    [picker setDate:now animated:YES];
    self.label.text = [now description];

    self.now = [NSDate date];
    self.label.text = [self.formatter stringFromDate:self.now];
}

-(void)refreshLabel
{
    NSDate *selected = [NSDate dateWithTimeIntervalSince1970:[[picker date] timeIntervalSince1970] - 1];
    [picker setDate:selected animated:YES];
    self.label.text = [self.formatter stringFromDate:selected];
}
@end

我几乎可以肯定地将问题追溯到refreshLabel方法。虽然我需要一些帮助,了解它为什么不起作用以及我可以做些什么来解决它。再次感谢你们!

0 个答案:

没有答案