iphone - 如何在 iPhone 中重置倒计时?

标签 iphone ios timer reset

我在 iPhone 应用程序中工作,使用 NSTimer 创建时间计数以在屏幕上设置,如最初 100,然后经过 99、98、97 等...如果我已经完成了可用耗时的游戏,那么我显示AlertView 成功完成,用户按确定按钮导航到上一个屏幕,然后再次转到游戏屏幕,耗时从之前耗时开始,如 66、65、64 等...我想,当用户转到游戏画面再次显示计时从100开始,如何解决这个问题,请帮助我

提前致谢

我试过这个:

- (void)viewDidLoad
{
   timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(elapsedTime) userInfo:nil repeats:YES];
}


-(void)elapsedTime
{
    static int i = 100;
    NSLog(@"i:%d",i);

    lbl.text = [NSString stringWithFormat:@"%d",i];    

    i--;
    if(i<0)
    {
        [timer invalidate];
        timer = nil;
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [timer invalidate];
    timer = nil;
    [self.navigationController popViewControllerAnimated:YES];
}

最佳答案

在.h文件中定义int i为类变量

 - (void)viewDidLoad
{
  timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(elapsedTime) userInfo:nil repeats:YES];
  i = 100;
}

-(void)elapsedTime
{ 
   i--;
   if(i<0)
   {
     //show alertView here
     [timer invalidate];
     timer = nil;
   }
}

关于iphone - 如何在 iPhone 中重置倒计时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12559267/

相关文章:

iphone - 一旦 App 进入后台,AVAssetWriter 就会失败

ios - Swift 中的 TableView 页脚

timer - Apache Flink - 如果 x 分钟没有收到数据,则发送事件

ios - Apple PushNotification : Need Server Side functionality Guide

iphone - 当我的 iPad 关闭时只收到最后的推送通知

iphone - 如何使用GAE作为后台服务器设计iPhone上的facebook id登录流程?

python - 如何在 10 秒后强制用户输入

iphone - 应该使用 NIB 或 iPhone 中的代码创建 View 吗?

ios - 在开始编辑时选择 UISearchBar 中的所有文本?

javascript - 如果选择框是,则每 x 秒播放一次声音?