Ошибка NSTimer в селекторе - PullRequest
0 голосов
/ 18 мая 2011

Я пытаюсь использовать NSTimer для установки задержки при доступе к методу из другого. Я имею в виду, что я хочу, чтобы метод movebricksdown был доступен через несколько секунд, а не прямо из моего метода findmatches

Но вместо @selector я получаю сообщение об ошибке.

Я использую

delaymovingbrickdowntimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(movebricksdown: x Y: y Direction: Dir) userInfo:nil repeats:YES];

чтобы поставить задержку.

Я знаю, что это сработает, если будет написано @selector (movebricksdown), но мне нужны значения x, y и Dir для обозначения метода movebricksdown.

что я делаю неправильно, когда использую

delaymovingbrickdowntimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(movebricksdown: x Y: y Direction: Dir) userInfo:nil repeats:YES];

пс. x, y и Dir - целые числа

заранее спасибо

* ***** 1024 1025 * UPDATE ******* ***************** * Я попробовал это так ...

//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 selector:@selector(moveBricksDown:) userInfo:dict repeats:NO];

***** UPDATE **************

Вместо этого я решил это с помощью executeselector.

Спасибо за вашу помощь:)

Ответы [ 3 ]

1 голос
/ 18 мая 2011

Две вещи:

a) Вы не можете передать аргумент методу, вызванному NSTimer.

b) Ваш синтаксис @selector неверен.

Одним из решений было бы передать 1011 * аргументов NSTimer 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];
0 голосов
/ 18 мая 2011

Передайте вам аргументы в объекте userInfo (в виде словаря или массива) и исправьте синтаксис селектора , как упоминал Джейкоб.Это должно выглядеть так.

@ selector (methodName) // без параметра

@ selector (methodName :) // принимает объект userInfo

Документация Apple по селекторам

0 голосов
/ 18 мая 2011


Если вы хотите передать x, y, Dir в вашем методе, то передайте его как информацию о пользователе ..

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];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...