简体   繁体   English

为NSArray中的对象运行NSTimer

[英]Running an NSTimer for objects in an NSArray

I have an NSArray of NSObjects . 我有一个NSArray NSObjects For each object in the NSArray , I'd like to run a timer until it finishes, then restart it for the next object. 对于NSArray中的每个对象,我想运行一个计时器,直到完成为止,然后为下一个对象重新启动它。

The objects in the NSArray will be used to populate UITextField s. NSArray的对象将用于填充UITextField

I've tried for (NSObject *myThing in self.ArrayOfMyThings) , it ends up displaying one timer correctly on screen while instantiating a bunch of others. 我尝试for (NSObject *myThing in self.ArrayOfMyThings) ,它最终在实例化许多其他计时器的同时在屏幕上正确显示了一个计时器。 Thank you for reading. 感谢您的阅读。 I welcome suggestions on how I might go about accomplishing this feat of software engineering ;) 我欢迎有关如何完成软件工程壮举的建议;)

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;
}

I would suggest creating a class property to keep track of the numeric index of the "current" item, eg: 我建议创建一个类属性来跟踪“当前”项的数字索引,例如:

@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. 开始时,将其初始化为0。然后,在“计时器已完成”例程中,只需增加索引,并确保您不在数组的末尾。 If you're done, then invalidate your timer. 如果完成,则invalidate计时器invalidate If not, just carry on with the new index value. 如果不是,则继续使用新的索引值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM