gpt4 book ai didi

ios - 为 NSArray 中的对象运行 NSTimer

转载 作者:行者123 更新时间:2023-11-29 12:22:49 25 4
gpt4 key购买 nike

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

NSArray 中的对象将用于填充 UITextField

我试过 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;
}

最佳答案

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

@property (nonatomic) NSInteger currentIndex;

使用它来识别您当前正在使用的数组中的哪一项。当您开始时,将其初始化为 0。然后,在“计时器完成”例程中,只需增加您的索引,确保您不在数组的末尾。如果您完成了,则使您的计时器无效。如果不是,则继续使用新的索引值。

关于ios - 为 NSArray 中的对象运行 NSTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30100973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com