2014-10-17 61 views
2

我想编写一个创建多个NSTimer的方法(nstimer的数量不会事先知道,代码应该是通用的),它会调用一个方法作为它的选择器,它将timeleft作为它的文本更新。基本上是倒计时。 现在对于许多定时器来说,许多标签必须存在,并且当事先知道定时器n标签(它们是相同的)的数量时,通过创建那么多的nstimer及其每个时间戳作为实例变量,对象,我已经完成了这项工作。Multiple NSTimer with individual timeleft

但是每个计时器都有自己的时间左移。 我的问题是,是否有可能保持多个未定量数量的nstimer来运行单个方法(所有定时器都调用该方法),其中每个定时器将具有其个别倒计时或其时间左移,定时器将运行多少次。 如果是这样,应该是什么逻辑。

+0

研究的NSTimer – Paulw11 2014-10-17 09:17:30

+0

了'userInfo'产权人已经尝试倒计时阵列的内容,即使我发每个NSTimer的timeleft作为其用户信息,其调用的方法不知道它从哪个定时器发送。 – 2014-10-17 09:18:21

+2

我会使用一个始终运行的计时器,间隔为1秒。然后在一个数组中保留单独的“timeleft”值,也许使用指向相关文本字段的相关指针,并在定时器激活方法的循环中执行timeleft递减和更新文本字段。这将更容易维护和运行。 – Droppy 2014-10-17 09:19:23

回答

1

好的,我解决了这个问题 我创建了一个数组,指向标签的指针,Droppy建议。

这是我的updateLabel方法,被单个计时器调用。

(void)updateLabel{ 
for(int i =0;i<[countdown count];i++){ 
int seconds,minute,hours,timeleft; 
    timeleft = [[countdown objectAtIndex:i]intValue]; 
    NSLog(@"%i",timeleft); 
seconds = timeleft%60; 
minute = timeleft/60; 
hours = minute /60; 
    UILabel *mylabel = [labelArray objectAtIndex:i]; 
    mylabel.text = [NSString stringWithFormat:@"%i:%i:%i",hours,minute,seconds]; 
    timeleft --; 
    NSLog(@"%@",countdown); 
    [countdown replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%i",timeleft]]; 
    NSLog(@"%@",countdown); 
if(timeleft ==0){ 
    [timer1 invalidate]; 
} 
} 
} 

这是

countdown = [[NSMutableArray alloc]initWithObjects:@"1000" ,@"1523",@"234",@"600",@"900", nil]; 

希望这有助于正面临这同样的问题

1

在这是“跑”的计时器对象,保持一个计时器对象,它始终在运行,这将保持“的timeleft”和“标签”实例自定义对象的名单进行更新:

LabelTime.h:

@interface LabelTime : NSObject 
@property NSTimeInterval timeLeft; 
@property UILabel *label; 
- (void)decrementTime:(NSTimeInteval)interval; 
@end 

LabelTime.m:

@interface LabelTime 

- (void)decrementTime:(NSTimeInterval)interval { 
    NSTimeInterval newTimeLeft = self.timeLeft - interval; 
    if (newTimeLeft >= 0.0) { 
     self.label.text = [NSString stringWithFormat:@"%d seconds left", (int)newTimeLeft]; 
     // Only if you want whole numbers    ^^    ^^^^^ 
    } 
    self.timeLeft = newTimeLeft; 
} 

@end 

someclass.m:

#define UPDATE_TIME 1.0 

@implementation SomeClass() { 
    NSTimer *_timer; 
    NSTimeInterval _lastTimeFired; 
    NSMutableArray *_labelTimes;  // array of LabelTime objects 
} 
- (void)_timerFired:(NSTimer *)timer; 
@end 

初始化它们的地方:

- (id)init { 
    self = [super init]; 
    if (self) { 
     _timer = [NSTimer scheduledTimerWithTimeInterval:UPDATE_TIME 
                target:self 
               selector:@selector(_timerFired:) 
               userInfo:nil 
               repeats:YES]; 
     _lastTimeFired = [[NSDate date] timeIntervalSinceReferenceDate]; 
     _labelTimes = [NSMutableArray new]; 
    } 
    return self; 
} 

- (void)_timerFired:(NSTimer *)timer { 
    NSTimeInterval now = [[NSDate date] timeIntervalSinceReferenceDate]; 
    NSTimeInterval interval = _lastTimeFired - now; 
    for (LabelTime *labelTime in _labelTimes) { 
     [labelTime decrementTime:interval]; 
    } 
    _lastTimeFired = now; 
} 

注:

  • 您可能要 “锁定” 读_labelTimes /写访问,以确保线程安全。