ios - iOS NSTimer触发两次

标签 ios nstimer

嗨,我正在尝试使用NSTimer创建一个倒计时,并使用标签进行查看。问题是标签下降了两位,我也不知道为什么。有任何想法吗?

这是我的代码

   - (void)viewDidLoad
   {
       NSLog(@"%@", self.chosenTime);
       [self startGame];
       [super viewDidLoad];

       NSString *timeString =  self.chosenTime;
       self.timer = [timeString intValue];
       self.countdown.text = timeString;
       // Do any additional setup after loading the view.
    }

    - (void)startGame
    {
        [self introCountdown];
        [self performSelector:@selector(goAnimation) withObject:nil afterDelay:3.0];
        NSString *timeString =  self.chosenTime;
        self.timer = [timeString intValue];
        [self performSelector:@selector(startCountdown) withObject:nil afterDelay:4.0];
    }

    - (void)startCountdown
    {
         //self.countdownTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(decrementTimer:) userInfo:nil repeats:YES];
         self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.00 target:self selector:@selector(decrementTimer:) userInfo:nil repeats:YES];
    }

    - (void)decrementTimer:(NSTimer *)timer
    {
       if(self.timer > 0)
       {
           self.timer = self.timer - 1;
           self.countdown.text = [NSString stringWithFormat:@"%d", self.timer];
       }
       else 
       {
            [self.countdownTimer invalidate];
            self.countdownTimer = nil;
        }
    }

最佳答案

此代码似乎是正确的。您可能正在其他地方更改标签的文本吗?

关于ios - iOS NSTimer触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10412155/

相关文章:

ios - UITableView 的圆上角

iphone - iOS:存储行和列的表

ios - 手动安装 Crypto swift 库。意外的平台状况

ios - 使用 arc 使 NSTimer 无效时遇到问题 - IOS

c++ - 将 NSTimer 添加到 C++ 类

ios - Swift Obj-c 模型

iphone - 使用 NSTimer 使按钮在一段时间后可见

cocoa - 为什么需要 NSRunLoop 来运行计时器?

ios - UIAlertView 在调用 loginButton 操作后出现太短