运行 NSArray 中对象的 NSTimer
Running an NSTimer for objects in an NSArray
我有 NSArray
个 NSObjects
。对于 NSArray
中的每个对象,我想 运行 一个计时器直到它完成,然后为下一个对象重新启动它。
NSArray
中的对象将用于填充 UITextField
s。
我试过 for (NSObject *myThing in self.ArrayOfMyThings)
,它最终在屏幕上正确显示了一个计时器,同时实例化了一堆其他计时器。感谢您阅读。我欢迎就如何完成这一软件工程壮举提出建议 ;)
我的计时器是在这个方法中实例化的:
- (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;
}
}
计时器使用此方法计数:
- (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" );
}
}
这个方法就是我手动停止定时器的方法:
- (IBAction)stopButton:(UIButton *)sender {
NSLog(@"stopButton pressed");
[self.timer invalidate];
self.timer = nil;
self.isPaused = YES;
}
我建议创建一个 class 属性 来跟踪 "current" 项目的数字索引,例如:
@property (nonatomic) NSInteger currentIndex;
使用它来确定您当前正在使用数组中的哪个项目。当您开始时,将其初始化为 0。然后,在 "timer is finished" 例程中,只需增加您的索引,确保您不在数组的末尾。如果你完成了,那么 invalidate
你的计时器。如果不是,则继续使用新的索引值。
我有 NSArray
个 NSObjects
。对于 NSArray
中的每个对象,我想 运行 一个计时器直到它完成,然后为下一个对象重新启动它。
NSArray
中的对象将用于填充 UITextField
s。
我试过 for (NSObject *myThing in self.ArrayOfMyThings)
,它最终在屏幕上正确显示了一个计时器,同时实例化了一堆其他计时器。感谢您阅读。我欢迎就如何完成这一软件工程壮举提出建议 ;)
我的计时器是在这个方法中实例化的:
- (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;
}
}
计时器使用此方法计数:
- (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" );
}
}
这个方法就是我手动停止定时器的方法:
- (IBAction)stopButton:(UIButton *)sender {
NSLog(@"stopButton pressed");
[self.timer invalidate];
self.timer = nil;
self.isPaused = YES;
}
我建议创建一个 class 属性 来跟踪 "current" 项目的数字索引,例如:
@property (nonatomic) NSInteger currentIndex;
使用它来确定您当前正在使用数组中的哪个项目。当您开始时,将其初始化为 0。然后,在 "timer is finished" 例程中,只需增加您的索引,确保您不在数组的末尾。如果你完成了,那么 invalidate
你的计时器。如果不是,则继续使用新的索引值。