2012-01-17 27 views
1

我正在嘗試更新從用戶出生日期到下一個日期計數的UILabel。我試圖更新標籤,以便他們可以看到它倒計時,但似乎無法使其工作。建議表示讚賞!通過計時器不工作更新UILabel

這裏就是我做的計時器:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeFormatted:) userInfo:nil repeats:NO]; 
} 

,這裏是從哪裏獲得的實際倒計時信息...

- (NSString *)timeFormatted:(int)totalSeconds 
{  
    NSTimeInterval theTimeInterval = [self numberOfSecondsToLive]; 

    NSCalendar *sysCalendar = [NSCalendar currentCalendar]; 

    NSDate *date1 = [[NSDate alloc] init]; 
    NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1]; 

    unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSSecondCalendarUnit; 

    NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0]; 

    _numberofSeconds.text = [NSString stringWithFormat:@" %dyears:%dMonths:%ddays:%dhour:%dminutes:%dseconds",[conversionInfo year],[conversionInfo month], [conversionInfo day], [conversionInfo hour], [conversionInfo minute], [conversionInfo second]]; 

    [date1 release]; 
    [date2 release]; 
} 

什麼情況是,它並顯示正確的倒計時信息,但它不會更新它。

預先感謝您!

回答

5

您啓動了計時器repeats:NO,您希望該計時器重複計時器爲repeats:YES

+0

謝謝!我對此很陌生,認爲這會讓我錯過一些簡單的事情。謝謝!! – sixstatesaway 2012-01-17 22:21:50

3

也許你想在這一行設置repeatsYES而不是NO

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

在一個側面說明,這條線是錯誤的:

- (NSString *)timeFormatted:(int)totalSeconds 

它應該是:

- (void)timeFormatted:(NSTimer *)timer