2011-04-04 85 views
-1

倒數計時器不起作用。它從屏幕上的'99'開始,它就停在那裏。它根本不動。倒數計時器不起作用

在我的頭文件中。

@interface FirstTabController : UIViewController { 
    NSTimer *myTimer; 
} 

@property (nonatomic, retain) NSTimer *myTimer; 

在我的.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]; 
} 

而且我在的dealloc無效 'myTimer'。那麼,有人可以告訴我我的代碼有什麼問題嗎?

回答

2

每個定時器的方法是叫你設置counterInt(回)到100

你可以做一個靜態變量

變化int counterInt = 100;static int counterInt = 100;

,當然你要節省時間counterInt中遞減的值。

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

,如果你需要這個方法以外的變量,你應該讓你的counterInt類的實例變量。

@interface FirstTabController : UIViewController { 
    int counterInt; 
} 

等等。

+0

謝謝..我忽略了.. – sicKo 2011-04-05 10:02:24