为NSArray中的对象运行NSTimer

时间:2021-10-05 21:20:57

I have an NSArray of NSObjects. For each object in the NSArray, I'd like to run a timer until it finishes, then restart it for the next object.

我有一个NSObjects的NSArray。对于NSArray中的每个对象,我想运行一个计时器直到它完成,然后为下一个对象重新启动它。

The objects in the NSArray will be used to populate UITextFields.

NSArray中的对象将用于填充UITextFields。

I've tried for (NSObject *myThing in self.ArrayOfMyThings), it ends up displaying one timer correctly on screen while instantiating a bunch of others. Thank you for reading. I welcome suggestions on how I might go about accomplishing this feat of software engineering ;)

我试过(在self.ArrayOfMyThings中的NSObject * myThing),它最终会在屏幕上正确显示一个计时器,同时实例化其他一些计时器。谢谢你的阅读。我欢迎有关如何完成这项软件工程专业的建议;)

My timer is instantiated in this method:

我的计时器在此方法中实例化:

- (IBAction)startButton:(UIButton *)sender {
    if (self.isPaused == YES) {
        NSLog(@"startButton pressed");
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                      target:[NSBlockOperation blockOperationWithBlock:^{
                [self counting];
        }]
                                                    selector:@selector(main)
                                                    userInfo:nil
                                                     repeats:YES];
        self.isPaused = NO;

    }
}

The timer counts using this method:

计时器使用此方法计数:

- (void)counting {
    // ** If the timer is RUNNING **
    if (self.isPaused == NO && secondsRemaining > 0) {
        secondsRemaining-=1;
        elapsedTime+=1;
        self.timerLabel.text = [self timeFormatted:secondsRemaining];
        NSLog(@"counting: isPaused = %@", self.isPaused ? @"YES" : @"NO" );

        if (secondsRemaining < 4 && secondsRemaining > 0) {
            [self voiceCountdownAlert];
        }
    // ** If the timer is FINISHED **
    } else if (self.isPaused == NO && secondsRemaining == 0) {
        [self resetTimer];
        [self voiceTimerCompleteAlert];
        secondsRemaining = elapsedTime;
        elapsedTime = 0;
        self.timerLabel.text = [self timeFormatted:secondsRemaining];
        NSLog(@"counting: The timer was reset");
    // ** If the timer is paused **
    } else if (self.isPaused == YES) {
        NSLog(@"counting: isPaused = %@", self.isPaused ? @"YES" : @"NO" );
    }
}

This method is how I stop the timer manually:

这种方法是我手动停止计时器的方法:

- (IBAction)stopButton:(UIButton *)sender {
    NSLog(@"stopButton pressed");
    [self.timer invalidate];
    self.timer = nil;
    self.isPaused = YES;
}

1 个解决方案

#1


I would suggest creating a class property to keep track of the numeric index of the "current" item, e.g.:

我建议创建一个类属性来跟踪“当前”项的数字索引,例如:

@property (nonatomic) NSInteger currentIndex;

Use this to identify which item in the array you are currently using. When you start, initialize this to 0. Then, in the "timer is finished" routine, just increment your index, make sure you're not at the end of the array. If you're done, then invalidate your timer. If not, just carry on with the new index value.

使用此选项可标识当前使用的阵列中的哪个项目。当你启动时,将其初始化为0.然后,在“计时器完成”例程中,只需递增索引,确保不在数组的末尾。如果你完成了,那么你的计时器就会失效。如果没有,只需继续使用新的索引值。

#1


I would suggest creating a class property to keep track of the numeric index of the "current" item, e.g.:

我建议创建一个类属性来跟踪“当前”项的数字索引,例如:

@property (nonatomic) NSInteger currentIndex;

Use this to identify which item in the array you are currently using. When you start, initialize this to 0. Then, in the "timer is finished" routine, just increment your index, make sure you're not at the end of the array. If you're done, then invalidate your timer. If not, just carry on with the new index value.

使用此选项可标识当前使用的阵列中的哪个项目。当你启动时,将其初始化为0.然后,在“计时器完成”例程中,只需递增索引,确保不在数组的末尾。如果你完成了,那么你的计时器就会失效。如果没有,只需继续使用新的索引值。

相关文章