2012-03-01 8 views
6

tengo este código:NSTimer userInfo. ¿Cómo está pasando el objeto al selector?

-(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 usuario sostiene un botón. Cuando el usuario lo suelta, el cronómetro se detendrá.

Pero yo estoy dando esto:

- [__ NSCFTimer intValue]: Selector no reconocido enviado a la instancia 0x4ae360

Pero si estoy en paasing userInfo una clase NSNumber, ¿por qué yo' m recibiendo el temporizador?

Gracias.

Respuesta

25

su método de acción temporizador debe tener este aspecto

-(void)rotateSelectedItem:(NSTimer*)sender 

Puede llegar a la userInfo haciendo

NSNumber *userInfo = sender.userInfo; 
2

No entendió la firma del selector que registra con el temporizador. El remitente es NSTimer*, no el objeto userInfo que se pasa en su constructor:

-(void)rotateSelectedItem:(NSTimer*)sender 
{ 
    float currAngle = [selectedItem currentRotation]; 
    if ([sender.userInfo intValue] == RDUtilitiesBarRotationLeft) 
    { 
     [selectedItem rotateImage:currAngle - 1]; 
    } 
    else 
    { 
     [selectedItem rotateImage:currAngle + 1]; 
    } 
} 
2

De la documentación:

El mensaje para enviar al destino cuando se dispara el temporizador. El selector debe tener la siguiente firma:

- (void)timerFireMethod:(NSTimer*)theTimer