【问题标题】:Timer decrement gets varied for each page load每次页面加载的计时器减量都会有所不同
【发布时间】:2012-01-25 21:28:01
【问题描述】:

我的项目有一个计时器,每次它递减 1 秒。但是,如果计数器第二次开始工作,它会减少 2 秒,第三次减少 3 秒等。我应该怎么做才能一直减少 1 秒?

-(void)viewDidAppear:(BOOL)animated { 

    count=15; //timer set as 15 seconds 
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:)userInfo:nil repeats:YES]; //for decrementing timer
}

- (void)updateCounter:(NSTimer *)theTimer { 
    count -= 1; 
    NSString *s = [[NSString alloc] initWithFormat:@"%d", count]; 
    if(count==0) // alert for time expiry 
    { 
       alert = [[UIAlertView alloc] initWithTitle:@"Time Out!!" message:@"Your time is expired." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
       [alert show]; 
       self.timer.hidden=YES; 
       [self dismissModalViewControllerAnimated:YES]; 
    } 

    else { 
      self.timer.text = s;
    } 

    [s release]; 
} 

【问题讨论】:

  • 我的代码是我的代码是 -(void)viewDidAppear:(BOOL)animated { count=15; //定时器设置为15秒 [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:)userInfo:nil repeats:YES]; //用于递减定时器 }
  • - (void)updateCounter:(NSTimer *)theTimer { count -= 1; NSString *s = [[NSString alloc] initWithFormat:@"%d", count]; if(count==0) // 超时提醒 { alert = [[UIAlertView alloc] initWithTitle:@"Time Out!!"消息:@“您的时间已过期。”委托:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [警报显示]; self.timer.hidden=YES; [自我dismissModalViewControllerAnimated:是]; } else { self.timer.text = s;} [s release]; }
  • 您可以将您的代码放入问题中,以便阅读。
  • 试过这样做,但我不能这样做,他们说它没有格式化或其他什么..对不起..
  • 我认为最好在 viewDidAppear 中检查计时器是否有效......所以最好尝试一下!

标签: iphone objective-c xcode nstimer


【解决方案1】:

根据您发布的内容,您正在创建多个计时器并且没有停止其中任何一个。因此,在 3 次之后,您每秒会触发 3 个计时器。

至少,当计时器达到零时,您希望使其无效:

[theTimer invalidate]

但您可能还需要考虑保留您创建的计时器(在@property 中),这样如果用户在您的计数器实际变为零之前以其他方式离开此视图,您就可以使其无效并释放它。

希望对您有所帮助。

【讨论】:

  • 是的,这对我有帮助。现在计时器运行良好。但是当我使用后退按钮时,如果我尝试停止它并返回相同的问题,则在计数器运行时会重复相同的问题。我会得到解决方案吗?
  • 好吧,每次你的视图出现时你都会启动一个新的计时器,所以你应该停止那个计时器(使其无效)并在视图将要消失时释放它(假设你保留它)(viewWillDisappear: )
【解决方案2】:

编辑
(正如我发布此消息一样,我注意到您已经知道接受了上述答案,并且似乎已经删除了您对@Firoze Lafeer 答案的“它不起作用”的评论。但我会以任何方式将其留在这里。)

运行下面的代码作为测试我没有得到你的问题。即使我没有无效,我也只是在日志中得到了多个输出。

而不是通过查看应用程序中的文本字段正在做什么来查看正在发生的事情。尝试使用如下所示的日志记录,看看这是否能让您更好地了解正在发生的事情。

-(IBAction)runTimer:(id)sender { // attached to a button
    [self invalidateTimer];
    count=15; //timer set as 15 seconds 
    //for decrementing timer
    countimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                target:self
                                              selector:@selector(updateCounter:) 
                                              userInfo:nil
                                               repeats:YES];
}

-(void)updateCounter:(NSTimer *)theTimer { 
    count -= 1; 
    NSLog (@"count =@%d", count);          
    if(count==0) // alert for time expiry 
    { 
        [self invalidateTimer];
    }
}

-(void)invalidateTimer {
    if ([countimer isValid]) {
        [countimer invalidate];
        NSLog(@"countimer invalidated ");
        countimer = nil;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    相关资源
    最近更新 更多