1

I want to give a parameter with an NSTimer. timertocallguest is an NSTimer made in the .h;

timertocallguest=[NSTimer scheduledTimerWithTimeInterval:1  target:self selector:@selector(gettheguestyouaskedforTOTURNON) userInfo:nil repeats:YES];

How can i pass parameters with the selector? At some selectors i could add withObject behind it to give parameters with it.. but i cant do it here, i just want to give an NSIndexPath with the function that i call.

Could annyone help me with this? Ty already,

Emre Akman
  • 1,212
  • 3
  • 19
  • 25
  • possible duplicate of [Passing parameters to the method called by a NSTimer](http://stackoverflow.com/questions/4011297/passing-parameters-to-the-method-called-by-a-nstimer) – jscs May 23 '11 at 07:36

2 Answers2

2

Your call is lacking the colon after the selectors name. It should read like this:

timertocallguest=[NSTimer scheduledTimerWithTimeInterval:1  target:self selector:@selector(gettheguestyouaskedforTOTURNON:) userInfo:nil repeats:YES];

If Xcode suggested the selector without the colon though, make sure the method you're trying to call actually takes an argument (of type id or NSIndexPath*).

Edit in response to comment:

The object (in this case myObject) needs to be passed in as the userInfo, like so:

timertocallguest=[NSTimer scheduledTimerWithTimeInterval:1  target:self selector:@selector(gettheguestyouaskedforTOTURNON:) userInfo:myOject repeats:YES];
Toastor
  • 8,980
  • 4
  • 50
  • 82
  • I was able to make the selector without the collon and it was working. so this is the right way? -(void)gettheguestyouaskedforTOTURNON:(NSIndexPath *)therow with the NSTimer timertocallguest=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(gettheguestyouaskedforTOTURNON:selectedIndexPath) userInfo:nil repeats:YES]; – Emre Akman May 23 '11 at 07:43
  • hmm i have got this now but i keep getting a NSFCTimer error.. -(void)gettheguestyouaskedforTOTURNON:(NSIndexPath *)therow{; timertocallguest=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(gettheguestyouaskedforTOTURNON:) userInfo:SelectedIndexPath repeats:YES]; SelectedIndexPath is an NSIndexPath object which i use the get the index of the row. – Emre Akman May 23 '11 at 08:09
  • Could you please post the entire error message here? This would be propably helpful... – Toastor May 23 '11 at 08:30
  • *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer row]: unrecognized selector sent to instance 0x4b6b200' *** Call stack at first throw: is this all you need? im doing exactly the same as i told im my last comment – Emre Akman May 23 '11 at 08:31
  • This kind of error appears if a message is being sent to an object which does not understand (implement a method for) it and is sometimes due to memory management problems. As such, I don't think this is related directly to your primary issue. Did this occur before you followed my instructions too? – Toastor May 23 '11 at 08:40
  • This was not happening before i followed your instructions, hmmm – Emre Akman May 23 '11 at 08:43
  • Could you please post the implementations of the gettheguestyouaskedforTOTURNON method and the method which hosts the instantiation of the timer as an update to your question? Additionally, please try the following: Add NSLog(@"Timer fired!"); to the beginning of your gettheguestyouaskedforTOTURNON method and then check the console for the Timer fired! message - does it appear and if so, how often? – Toastor May 23 '11 at 08:49
  • i cant post the whole method because its connecting to an https server with static parameters but i can say that the method does gets fired off and the app crashes.. well what im basicly trying to do is the following.. i have got an table with Guests(VM's) that i can turn ON/OFF/Reboot when i try to do an action on 1 guest it works perfectly but when i try to send an action to 2 guests the table responds weird and the app crashes its doing this because its overwriting the value of the SelectedIndexPath when i send the action again.. so this is why im trying to do this with parameters. – Emre Akman May 23 '11 at 09:01
  • intheitemname = [dict objectForKey:@"name"]; myPowerOnOrNot = [dict objectForKey:@"powerstatus"]; [tablearray replaceObjectAtIndex:therow.row withObject:intheitemname]; myString = [NSString stringWithFormat:@"%d", [myPowerOnOrNot intValue]]; if([myString isEqualToString:@"1"]) { [tablearrayPOWERSTATUS replaceObjectAtIndex:therow.row withObject:[NSString stringWithFormat:@"%@",@"ON"]]; } when i debug it crashes where the line therow.row is. – Emre Akman May 23 '11 at 09:02
  • Well in this case you might want to hand over the parameters in a completely different way - by creating a property within the controller. The Timer will retain the original indexpath object, to be honest I'm not sure if this may cause your problem, but it feels like a "dirty" solution in this case, at least to me... – Toastor May 23 '11 at 09:11
  • hmmm okej, i will try out a few other things but thank you very much for you help! i realy appreciated it =) – Emre Akman May 23 '11 at 09:22
0

I have got a similar problem, when I called a method from - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath using NSTimer.

Cable W
  • 633
  • 1
  • 8
  • 17