I'm trying to use an NSTimer to put an delay when accessing a method from inside another one. What I mean is that I want the method movebricksdown to be accessed a few seconds later instead of right away from my findingmatches method
But I get an error message instead on the @selector part.
I use
delaymovingbrickdowntimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(movebricksdown: x Y: y Direction: Dir) userInfo:nil repeats:YES];
to put the delay.
I know that it will work if it says @selector(movebricksdown) but I need the x, y and Dir values to tag along to the movebricksdown method.
what am I doing wrong when I use
delaymovingbrickdowntimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(movebricksdown: x Y: y Direction: Dir) userInfo:nil repeats:YES];
?
ps. x, y and Dir are all integers
thanks in advance
***UPDATE************************* I tried it like this...
//the method movebricksdown
-(void)movebricksdown: (int) x Y: (int) y Direction: (int) Dir
//The method that is called from the NSTimer statement
- (void) moveBricksDown:(NSTimer *) timer {
NSDictionary *dict = [timer userInfo]; //warning: local declaration of 'timer' hides instance variable
[self movebricksdown:[[dict objectForKey:@"x"] intValue] Y:[[dict objectForKey:@"y"] intValue] Direction:[dict objectForKey:@"Dir"]]; //warning: passing argument 3 of 'movebricksdown:Y:Direction:' makes integer from pointer without a cast
}
NSNumber *newX = [NSNumber numberWithInt:x];
NSNumber *newY = [NSNumber numberWithInt:y];
NSNumber *newDir = [NSNumber numberWithInt:Dir];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:newX, @"x", newY, @"y", newDir, @"dir", nil];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selec开发者_运维知识库tor:@selector(moveBricksDown:) userInfo:dict repeats:NO];
*****UPDATE**************
I solved it with performselector instead.
Thanks any way for all of your help :)
Two things:
a) You cannot pass an argument to a method called by an NSTimer
.
b) Your @selector
syntax is incorrect.
One solution would be to pass off an NSDictionary
of arguments to the NSTimer
's userInfo
:
- (void) moveBricksDown:(NSTimer *) timer {
NSDictionary *dict = [timer userInfo];
[self movebricksdown:[[dict objectForKey:@"x"] intValue] y:[[dict objectForKey:@"y"] intValue] Direction:[dict objectForKey:@"dir"]];
}
#define NUMINT(x) [NSNumber numberWithInt:x]
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:NUMINT(x), @"x", NUMINT(y), @"y", Dir, @"dir", nil];
delaymovingbrickdowntimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(moveBricksDown:) userInfo:args repeats:YES];
Pass you arguments in the userInfo object (as a dictionary or an array) and fix the selector syntax as Jacob mentioned. It should like this.
@selector(methodName) // no parameter
@selector(methodName:) // takes the userInfo object
Apple Documentation on Selectors
If you want to pass x,y,Dir in your method then pass it as user info..
NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@"x_value",y,@"y_value",dic,@"youdic",nil]; //Like this you can set your x,y and dic value and can get in movebricksdown method
delaymovingbrickdowntimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(movebricksdown) userInfo:dic repeats:YES];
精彩评论