Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS NSTimer发射两次_Ios_Nstimer - Fatal编程技术网

iOS NSTimer发射两次

iOS NSTimer发射两次,ios,nstimer,Ios,Nstimer,嗨,我正在尝试使用NSTimer创建一个倒计时,并使用标签查看它。问题是标签在两个小时内就掉了,我不知道为什么。有什么想法吗 这是我的密码 - (void)viewDidLoad { NSLog(@"%@", self.chosenTime); [self startGame]; [super viewDidLoad]; NSString *timeString = self.chosenTime; self.

嗨,我正在尝试使用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;
        }
    }


抱歉误解,代码是正确的,这似乎是其他地方的UI问题或处理标签,如果可能,请使用xib附加您的源代码

抱歉误解,代码是正确的,这似乎是其他地方的UI问题或处理标签,如果可能的话,将您的源代码附加到xib上

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

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

您可以将代码发布到您称为startCountDown的位置吗?嗯。。你能尝试显式调用startCountDown([self startCountDown])并查看它是否有效吗?顺便说一句,应该首先在viewDidLoad()中执行[super viewDidLoad],然后初始化self.timer两次:一次在viewDidLoad中,一次在StartName中。@Jonas Byström不应该是问题。你能在调用startCountDown的地方发布代码吗?嗯。。您可以尝试显式调用startCountDown([self startCountDown])并查看它是否有效吗?顺便说一句,应该首先在viewDidLoad()中执行[super viewDidLoad]您初始化self.timer两次:一次在viewDidLoad中,一次在StartName中。@Jonas Byström不应该是问题所在,这不是问题的答案。这不起作用,它会在两个小时内倒计时,但它只会在我想要的时候倒计时一次,所以标签会在每个小时倒计时second@Shahid你不应该在不保留原始答案的情况下编辑你的答案内容或将新内容标记为“编辑”。这使得以后的读者不可能关注讨论。这不是问题的答案。这不起作用。它在两个小时内倒计时,但它只在我想要的时候倒计时一次,所以标签每小时倒计时一次second@ShahidAslam您不应该在不保留原始内容或将新内容标记为“编辑”的情况下编辑您的答案。这使得以后的读者无法跟上讨论。我在视图中对其进行了更改,以显示值,然后希望它从该值开始每秒钟倒数一次假设,您有
startValue=10。你想给作品贴上smth标签,比如10,9,…,0。好的,你必须增加你的值(
startValue=11;
),因为计时器在计数器递减后改变标签的文本。我在视图中改变它以显示值,然后希望它从该值开始每秒钟倒计时假设,你有
startValue=10。你想给作品贴上smth标签,比如10,9,…,0。好的,您必须增加您的值(
startValue=11;
,在这种情况下),因为计时器会在计数器递减后更改标签的文本。