Iphone 倒计时计时器赢了';行不通
我的倒计时器坏了。它从屏幕上的“99”开始,然后就停在那里了。它一点也不动 在我的头文件中Iphone 倒计时计时器赢了';行不通,iphone,timer,nstimer,countdown,Iphone,Timer,Nstimer,Countdown,我的倒计时器坏了。它从屏幕上的“99”开始,然后就停在那里了。它一点也不动 在我的头文件中 @interface FirstTabController : UIViewController { NSTimer *myTimer; } @property (nonatomic, retain) NSTimer *myTimer; 在我的.m文件中 - (void)observeValueForKeyPath:(NSString *)keyPath
@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”无效。那么,谁能告诉我我的代码有什么问题吗 每次调用计时器方法时,都将
countint
(返回)设置为100
你可以把它变成一个静态变量
更改int计数器int=100代码>进入静态int计数器int=100代码>
当然,你必须在计数器中保存递减的值
- (void)countDown {
static int counterInt = 100;
counterInt = counterInt - 1;
lblCountdown.text = [NSString stringWithFormat:@"%d", counterInt];
}
如果需要此方法之外的变量,则应将countint设置为类的实例变量
@interface FirstTabController : UIViewController {
int counterInt;
}
等等