2011-04-04 2 views
-1

Mon compte à rebours ne fonctionnera pas. Cela a commencé avec '99' sur l'écran et ça s'arrête là. Ça ne bouge pas du tout.Le compte à rebours ne fonctionnera pas

Dans mon fichier d'en-tête.

@interface FirstTabController : UIViewController { 
    NSTimer *myTimer; 
} 

@property (nonatomic, retain) NSTimer *myTimer; 

Dans mon fichier .m

- (void)observeValueForKeyPath:(NSString *)keyPath 
        ofObject:(id)object 
        change:(NSDictionary *)change 
        context:(void *)context { 
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES]; 
} 

- (void)countDown { 
    int counterInt = 100; 

    int newTime = counterInt - 1; 
    lblCountdown.text = [NSString stringWithFormat:@"%d", newTime]; 
} 

Et j'infirment 'myTimer' dans mon dealloc. Alors, quelqu'un peut-il me dire ce qui ne va pas avec mon code.

Répondre

2

chaque fois que votre méthode de minuterie est appelée vous définissez counterInt (retour) à 100.

vous pourriez en faire une variable statique

changement int counterInt = 100; dans static int counterInt = 100;

et bien sûr, vous devez sauver la valeur décrémentée dans counterInt.

- (void)countDown { 
    static int counterInt = 100; 
    counterInt = counterInt - 1; 
    lblCountdown.text = [NSString stringWithFormat:@"%d", counterInt]; 
} 

si vous avez besoin de la variable en dehors de cette méthode, vous devez faire counterInt une variable d'instance de votre classe.

@interface FirstTabController : UIViewController { 
    int counterInt; 
} 

et ainsi de suite.

+0

Merci .. J'ai oublié que .. – sicKo