2011-04-04 71 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