I have a method that have input variable and I need to schedule this method usingNSTimer
Unfortunately when I try to make the idea I got some error
My code is the following:
My method:
-(void)movelabel:(UILabel *)label {
}
I make my scheduling using the following:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(movelabel:myLbabeName) userInfo:nil repeats:YES];
But, I got the following error:
error: expected ':' before ')' token
In other case (case of method without input variable i'm calling the timer like the following:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(myMethodNameWithoutVariable) userInfo:nil repeats:YES];
Regards
The selector you give to scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
does not take arbitrary arguments. It should be either a selector without parameter or a selector with a single parameter of type (NSTimer *)
.
That means you can't directly call moveLabel:
with your parameter myLbabeName
.
You could use the userInfo
dictionary with an intermediary method like this:
(timerRef
is a NSTimer
class variable)
timerRef = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(timerMovelabel:)
userInfo:[NSDictionary dictionaryWithObject:myLbabeName
forKey:@"name"]
repeats:YES];
and
- (void)timerMovelabel:(NSTimer *)timer {
[self movelabel:[[timer userInfo] objectForKey:@"name"]];
}
EDIT
If you want to stop the timer, keep a reference to it and call [timerRef invalidate]