2010-11-10 7 views
0

En essayant de faire le changement de textLabel à 12h toutes les 24 heures quelqu'un peut-il m'aider à configurer ce code?Changement de textLabel pour changer à un moment précis

Ceci est le code que j'utilise actuellement.

h fil

NSTimer *timer; 

IBOutlet UILabel *textLabel; 
fichier

m

(void)onTimer { 
static int i = 0; 

if (i == 0) { textLabel.text = @"iphone app"; } 

else if (i == 1) { textLabel.text = @" Great App!"; } 

else if (i == 2) { textLabel.text = @" WOW!"; } 

else { textLabel.text = @" great application again!!"; i = -1; } 

i++; } 

timer =[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 

Répondre

0

Essayez d'utiliser ce code:

NSDate *date = [NSDate date]; //current date 

NSTimeInterval secondsInDay = 24 * 60 * 60; 

NSCalendar *calendar = [NSCalendar currentCalendar]; 

NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date]; 
if([components hour] >= 12) { 
    //set next day - by adding 24 hours to current day 
    NSDate *nextDay = [NSDate dateWithTimeInterval:secondsInDay sinceDate:date]; 
    //recalculate components 
    components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:nextDay]; 
} 

NSDateComponents *newDateComponents = [[NSDateComponents alloc] init]; 
[newDateComponents setYear:[components year]]; 
[newDateComponents setMonth:[components month]]; 
[newDateComponents setDay:[components day]]; 
[newDateComponents setHour:12]; 
[newDateComponents setMinute:0]; 
[newDateComponents setSecond:0]; 

NSDate *firingDate = [calendar dateFromComponents:newDateComponents]; 
[newDateComponents release]; 

NSTimer *timer = [[NSTimer alloc] initWithFireDate:firingDate interval:secondsInDay target:self selector:@selector(onTimer:) userInfo:nil repeats:YES]; 
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; 
[timer release]; 

Ensuite implémenter la méthode ontimer:

- (void) onTimer:(NSTimer)timer { 
    self.textLabel.text = @"Greetings from Poland"; //;) 
} 
Questions connexes