have a reference to an object inside NSTimer selector

34 views Asked by At

i have more than 10 objects of the same object type, lets call it Car type. in my class, for each object i need to start a NSTimer.

//for object 1:
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];

//for object n:
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];

[EDIT] the timers are fired separately not at the same time.

However i have only one method for the 10 objects:

- (void)timerTick:(NSTimer *)timer
{
//here i need the reference to the object x (car) in order to display the timer and the name of the car for instance.
}

how can i achieve this, please? im thinking of subclassing NSTimer and adding a new property (car) to it.

CustomTimer *mytimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
- (void)timerTick:(CustomTimer *)timer
{
NSLog(@"%@", timer.car.name);
}

thanks

0

There are 0 answers