使 Objective-C iphone 中的 ntimer 无效
我在使 NSTimer
失效时遇到问题。 我使用以下函数初始化 NSTimer
。
-(void) initLTTimer{
[self shutLTTimer];
[self setQuestion:questionCounter];
isQuestionAttempt=NO;
tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
NSLog(@"Timer Initialized");
}
此外,我在其选择器中调用了一个函数updateLeftTime
。
- (void)updateLeftTime:(NSTimer *)theTimer {
NSLog(@"%d",timeCounter);
timeCounter+=1;
tfLeftTime.text=[NSString stringWithFormat:@"%d", (QUESTION_TIME_LIMIT-timeCounter)];
if (timeCounter>=QUESTION_TIME_LIMIT) {
if (isQuestionAttempt==NO) {
[self increaseDropShots];
}
[self setQuestionBg];
timeCounter=0;
[self shutLTTimer];
[self updateQuestion:nil];
}
}
在上面的函数中调用了这个函数[selfincreaseDropShots];
。 这是这个函数的代码
-(void)increaseDropShots
{
NSString *imgName = @"DactiveRetake";
if (IS_IPAD) {
imgName = [imgName stringByAppendingString:@"_ipad"];
}
wrongAttemp+=1;
for (int i =1; i<=wrongAttemp; i++ )
{
UIImageView *img=(UIImageView *)[self.view viewWithTag:i+50];
[img setImage:[UIImage imageNamed:imgName]];
}
NSLog(@"Question dropped counter: %d",wrongAttemp);
if (wrongAttemp == 3)
{
[self shutLTTimer];
[CommonFunctions initGlobalVars];
[Bgplayer stop];
[Bgplayer release];
OverPageViewController *opvc=[[OverPageViewController alloc] initWithNibName:[CommonFunctions getXIBFile:@"OverPageViewController"] bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:opvc animated:YES];
[opvc release];
}
}
在这个函数中,我正在杀死这个计时器,但我做不到。
-(void) shutLTTimer{
if ([tmLeftTime isValid]) {
[tmLeftTime invalidate];
tmLeftTime=nil;
}
} 这是我的应用程序的整个场景
请帮我看看问题是什么。
I am having issue with invalidating a NSTimer
.
I initialize a NSTimer
with below function.
-(void) initLTTimer{
[self shutLTTimer];
[self setQuestion:questionCounter];
isQuestionAttempt=NO;
tmLeftTime=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLeftTime:) userInfo:nil repeats:YES];
NSLog(@"Timer Initialized");
}
further I called a function updateLeftTime
in its selector.
- (void)updateLeftTime:(NSTimer *)theTimer {
NSLog(@"%d",timeCounter);
timeCounter+=1;
tfLeftTime.text=[NSString stringWithFormat:@"%d", (QUESTION_TIME_LIMIT-timeCounter)];
if (timeCounter>=QUESTION_TIME_LIMIT) {
if (isQuestionAttempt==NO) {
[self increaseDropShots];
}
[self setQuestionBg];
timeCounter=0;
[self shutLTTimer];
[self updateQuestion:nil];
}
}
This function [self increaseDropShots];
called in above function.
here is the code of this function
-(void)increaseDropShots
{
NSString *imgName = @"DactiveRetake";
if (IS_IPAD) {
imgName = [imgName stringByAppendingString:@"_ipad"];
}
wrongAttemp+=1;
for (int i =1; i<=wrongAttemp; i++ )
{
UIImageView *img=(UIImageView *)[self.view viewWithTag:i+50];
[img setImage:[UIImage imageNamed:imgName]];
}
NSLog(@"Question dropped counter: %d",wrongAttemp);
if (wrongAttemp == 3)
{
[self shutLTTimer];
[CommonFunctions initGlobalVars];
[Bgplayer stop];
[Bgplayer release];
OverPageViewController *opvc=[[OverPageViewController alloc] initWithNibName:[CommonFunctions getXIBFile:@"OverPageViewController"] bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:opvc animated:YES];
[opvc release];
}
}
In this function I am killing this timer but I am unable.
-(void) shutLTTimer{
if ([tmLeftTime isValid]) {
[tmLeftTime invalidate];
tmLeftTime=nil;
}
}
This is the whole scenario of my application
Please help me what is the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在评论中对话后回答
代码是否确实到达行,
[tmLeftTime invalidate];
?您可以使用断点或 NSLog 来检查这一点。Answered after conversation in comments
Does the code definitely get to the line,
[tmLeftTime invalidate];
? You can check this by using a breakpoint or anNSLog
.