2012-12-04 25 views
0

可能重複:
Arguments in @selector如何調用超過2個參數參數的方法來選擇

如何調用超過2個參數參數的方法來選擇, 說對於EX我有這樣的方法

-(void)GetTheData:(NSString *)str1 :(NSString *)str2 

現在我需要在@ selector中的以下定時器中調用此方法。如何調用?

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(**HowCanICallHere**) userInfo:nil repeats:YES]; 
+2

[ PLZ通過這個](http://stackoverflow.com/questions/1349740/arguments-in-selector) –

+1

這是不好的做法,使用該風格的函數名稱。你應該給這個函數的每個部分一個名字。在你的例子中,沒有真正的方法來知道str2是什麼,或者可以從中得到什麼類型的數據。 – Fogmeister

回答

0

可以使用此:

的NSTimer *計時器= [的NSTimer scheduledTimerWithTimeInterval:0.0目標:自選擇器:@selector(GetTheData::) USERINFO:無重複:YES];

+0

如何通過sujay詢問如何在此方法中傳遞兩個以上的參數? – Atif

+0

Atif,我只是展示瞭如何將選擇器用於多個參數。現在,您的查詢是什麼? –

+0

你看過這個問題嗎? – Atif

2

通行證NSDictionary作爲參數傳遞給目標方法

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"parameter1",@"2",@"parameter2", nil]; 

     [ NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(myFunction:) userInfo: dictionary repeats:NO]; 

另外檢索在所述目標函數

-(void)myFunction:(NSTimer *)timer{ 
    NSLog(@" dict : %@",timer.userInfo); 
} 

這樣就可以在多個參數可以通過添加更多的keyValue對被傳遞的參數NSDictionary

0

我知道如何使用它與執行選擇器,所以也許你可以通過以下方式:

-(void)GetTheData1:(NSString *)str1 GetTheData2:(NSString *)str2 

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(mymethod) userInfo:nil repeats:YES]; 

- (void) mymethod { 
[self performSelector:@selector(GetTheData1:GetTheData2:) withObject:str1 withObject:str2]; 

} 
2

我不確定你可以。出於同樣的原因,「userInfo」選項給予我們,以傳遞多個參數。可以方便地與兩個對象執行通過創建的字典:

NSDictionary *dict = [NSDictionary dictionaryWithObjects:objArray andKeys:keyArray]; 

並傳遞字典作爲USERINFO對象的方法:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(urTimerMethod:) userInfo:dict repeats:YES]; 

定義方法爲:

- (void)urTimerMethod:(NSTimer *)timer { 
     NSDictionary *dict = [timer userInfo]; 
}