2012-05-02 59 views
1

嗨我想使用NSTimer创建一个倒计时,并使用标签查看此。问题在于标签出现在两个标签中,我不知道为什么。有任何想法吗?iOS 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; 
     } 
    } 
+1

你可以发布你调用startCountDown的代码吗? – giorashc

+0

嗯..你可以尝试显式调用startCountDown([self startCountdown])并查看它是否工作? – giorashc

+1

BTW [super viewDidLoad]应该首先在viewDidLoad()中执行 – giorashc

回答

-1

很抱歉的误解,代码是正确的,这似乎是一个UI问题,或者从其他地方搬运标签,附上你的源与此代码似乎厦门国际银行的,如果可能的

+0

这不是问题的答案。 –

+0

这是不正确的 – giorashc

+0

这不起作用它在两个仍然倒计时,但它只做它一旦我想它,所以标签每秒倒计时 – iamlukeyb

0

是正确的。也许你正在从别的地方改变标签的文字?

+0

我正在改变它在视图中卸载显示值,然后希望它倒数第二次从该值 – iamlukeyb

+0

假设,你有'startValue = 10;'。而且你想标记的作品像10,9,...,0那样。你必须增加你的值(在这种情况下''startValue = 11;'),因为计时器递减后,计时器改变标签的文本。 – demon9733