Nstimer with method that have variable
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]
You need to add ':' after the parameter, i.e.
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(movelabel:myLbabeName:) userInfo:nil repeats:YES];
You can't pass ur lable as parameter with selector...There should be either one parameter which will be id or no parameter..
here you have to use
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(movelabel:) userInfo:nil repeats:YES];
or
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(movelabel) userInfo:nil repeats:YES];
if you use the first on then you can get you timer and action defination will be like
- (void) moveLable :(id)sender {
}
sender will be timer.
anyways why you need your lable as parameter. you can directly access your lable if you declare it in .h file.
精彩评论