0

Possible Duplicate:
Arguments in @selector

How to call parameterized method with more than 2 parameters to the selector, say for EX i have a method like this

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

Now i need to call this method in the following timer inside the @selector.How can i call??

NSTimer  *timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(**HowCanICallHere**) userInfo:nil repeats:YES];
Community
  • 1
  • 1
sujay
  • 1,845
  • 9
  • 34
  • 55
  • 2
    [Plz go through this](http://stackoverflow.com/questions/1349740/arguments-in-selector) – Mehul Mistri Dec 04 '12 at 12:42
  • 1
    It's bad practise to use that style of function name. You should give each part of the function a name. In your example there is no real way of knowing what str2 is or what sort of data can be expected from it. – Fogmeister Dec 04 '12 at 13:28

4 Answers4

2

Pass NSDictionaryas a parameter to the target method

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

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

Further retrieving the parameters in the target function

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

This way you may multiple parameters can be passed by adding more keyValue pairs in the NSDictionary

AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
2

I'm not sure that you can. For the same reason "userInfo" option is given to us, to pass more than one parameter. You can easily implement that by creating a dictionary with two objects:

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

and pass that dictionary as userInfo object to the method:

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

define the method as:

- (void)urTimerMethod:(NSTimer *)timer {
        NSDictionary *dict = [timer userInfo];
}
Ajeet
  • 892
  • 1
  • 9
  • 22
0

You can use this:

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

Mohit_Jaiswal
  • 840
  • 6
  • 10
0

I know how to use it with a perform-selector, so maybe you could do it the following way:

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

}
Max
  • 572
  • 2
  • 6
  • 23