2

您好我正在尝试使用 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;
        }
    }
4

2 回答 2

0

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

于 2012-05-02T10:54:10.897 回答
-1

对不起,误解,代码是正确的,这似乎是一个 UI 问题或从其他地方处理标签,如果可能的话,将您的源代码附上 xib

于 2012-05-02T10:51:30.700 回答