2010-08-04 24 views
0

J'ai créé une minuterie où je convertis le temps restant en une chaîne avec un format de: "mm: ss" et quand la valeur de chaîne de l'heure est 00:00, je voudrais invalider le minuteur. Pour une raison quelconque, cela ne fonctionne pas, même lorsque je me connecte le temps restant, je vois que la valeur a le format correct, donc je ne sais pas pourquoi ma branche "if" dans "countTime:" n'est jamais entrée.Invalider NSTimer ne fonctionne pas

Quelqu'un peut-il aider?

-(id) init { 
    if(self = [super init]) { 
     NSDate *date = [[NSDate alloc] init]; 
     self.intervalLength = date; 
     [date release]; 

     NSString *timeRem = [[NSString alloc]init]; 
     self.timeRemaining = timeRem; 
     [timeRem release]; 


     formatter = [[NSDateFormatter alloc] init]; 
     [formatter setDateFormat:@"mm:ss"]; 
     notification = [NSNotification notificationWithName:@"timer" object:self]; 
     notificationForEnd = [NSNotification notificationWithName:@"timerFinished" object:self]; 
     NSMutableDictionary *info = [[NSMutableDictionary alloc] init]; 
     [info setObject:formatter forKey:@"format"]; 
     [info setObject:notification forKey:@"notification"]; 
     [info setObject:notificationForEnd forKey:@"notificationEnd"]; 
     NSTimer *timer = [[NSTimer alloc] init]; 
     timer = [[NSTimer alloc] init]; 
     timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(countTime:) userInfo:info repeats:YES]; 
     self.intervalTimer = timer; 
    } 
    return self; 
} 

-(void) startIntervalCountDown { 
    runLoop = [NSRunLoop currentRunLoop]; 
    [runLoop addTimer:self.intervalTimer forMode:NSDefaultRunLoopMode]; 
} 

-(void) countTime:(NSTimer*)timer { 
    if(self.timeRemaining == @"00:00") { 
     [timer invalidate]; 
     timer = nil; 
     [[NSNotificationCenter defaultCenter] postNotification:[[timer userInfo] objectForKey:@"notificationEnd"]]; 
    } 
    d = [self.intervalLength dateByAddingTimeInterval: -1.0]; 
    self.intervalLength = [d copy]; 
    self.timeRemaining = [[[timer userInfo] objectForKey:@"format"] stringFromDate:d]; 
    [[NSNotificationCenter defaultCenter] postNotification:[[timer userInfo] objectForKey:@"notification"]]; 

Répondre

2

Vous comparez l'égalité des pointeurs plutôt que l'égalité des chaînes. Vous voulez

[self.timeRemaining isEqualToString:@"00:00"]