Nstimer userinfo.¿Cómo se pasa el objeto al selector?
Pregunta
Tengo este código:
-(void)startRotation:(RDUtilitiesBarRotation)mode {
rotationTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(rotateSelectedItem:) userInfo:[NSNumber numberWithInt:mode] repeats:YES];
}
-(void)rotateSelectedItem:(NSNumber*)sender {
float currAngle = [selectedItem currentRotation];
if ([sender intValue] == RDUtilitiesBarRotationLeft) {
[selectedItem rotateImage:currAngle - 1];
}
else {
[selectedItem rotateImage:currAngle + 1];
}
}
-(void)stopRotation {
[rotationTimer invalidate];
rotationTimer = nil;
}
El objetivo es comenzar a girar una vista mientras el usuario tiene un botón.Cuando el usuario lo libera, el temporizador se detendrá.
pero estoy dando esto:
- [__ __ NSCFTIMER INTVALUE]: Selector no reconocido enviado a la instancia 0x4ae360
Pero si estoy empatando en la clase userinfo a nsnumber, ¿por qué estoy recibiendo el temporizador?
gracias.
Solución
Your timer action method should look like this
-(void)rotateSelectedItem:(NSTimer*)sender
You can get at the userInfo by doing
NSNumber *userInfo = sender.userInfo;
Otros consejos
You misunderstood the signature of the selector that you register with the timer. The sender is NSTimer*
, not the userInfo object that you pass into its constructor:
-(void)rotateSelectedItem:(NSTimer*)sender
{
float currAngle = [selectedItem currentRotation];
if ([sender.userInfo intValue] == RDUtilitiesBarRotationLeft)
{
[selectedItem rotateImage:currAngle - 1];
}
else
{
[selectedItem rotateImage:currAngle + 1];
}
}
From the documentation:
The message to send to target when the timer fires. The selector must have the following signature:
- (void)timerFireMethod:(NSTimer*)theTimer