2011-05-18 82 views
0

我想使用NSTimer從另一個訪問方法時,延遲。我的意思是,我希望movebricksdown的方法可以在幾秒鐘後被訪問,而不是立刻從我的findmatches方法中找到。在選擇器NSTimer錯誤

但是,我在@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]; 

ps。 X,Y和導演都是整數提前

***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* ** * ** * * * * ** * *

我用performselector代替它解決了它。

感謝任何方式對所有的幫助:)

+0

'[字典objectForKey:@「風向「]'應該是'[[dict objectForKey:@」Dir「] intValue]' – 2011-05-20 07:14:35

回答

1

兩件事情:

a)您不能參數傳遞到由NSTimer調用的方法。

b)您的@selector語法不正確。

一個解決辦法是到的參數的NSDictionary通關到NSTimeruserInfo

- (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

thanx快速響應...我會試一試... – iphonedevonthemake 2011-05-18 09:07:27

+0

我用你給我的方式更新了問題。我得到了兩個警告 – iphonedevonthemake 2011-05-20 06:17:29

+0

我修正了第一個警告,但我仍然得到這個警告://警告:傳遞'movebricksdown:Y:Direction:'的參數3,而不是在我嘗試調用movebricksdown :x Y:y Dir:Dir方法 – iphonedevonthemake 2011-05-21 05:05:28

0


如果你想通過X,Y,迪爾在你的方法,然後把它作爲用戶信息..

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]; 
+0

thanx用於快速響應...我會試一試... – iphonedevonthemake 2011-05-18 09:07:44

+0

當我嘗試此操作時收到警告......警告:傳遞'dictionaryWithObjectsAndKeys:'的參數1使從整數指針沒有演員 – iphonedevonthemake 2011-05-18 15:46:52

0

通行證你在用戶信息對象參數(作爲字典或陣列)和fix the selector語法雅各布提及。它應該是這樣的。

@selector(方法名)//沒有參數

@selector(方法名:) //取 USERINFO對象

Apple Documentation on Selectors

+0

thanx的快速反應...我會試一試... – iphonedevonthemake 2011-05-18 09:07:33

+0

@iphonedevonthemake有沒有任何答案幫助你?只需單擊複選標記即可將其標記爲已接受的解決方案或將其投票。請檢查您的其他問題以獲取可能的解決方案。謝謝! :)(是的,這是複製粘貼,所以新用戶意識到投票系統。) – 2011-05-18 09:08:38

+0

我還沒有嘗試過呢......我會盡快回家。如果它的工作,那麼我一定會接受答案,並將其投票;) – iphonedevonthemake 2011-05-18 09:09:53