NSTimer userInfo. How does an object move to a selector?

I have this code:

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

The goal is to start rotating the view while the user holds the button. When the user releases it, the timer stops.

But I give this:

- [__ NSCFTimer intValue]: unrecognized selector sent to instance 0x4ae360

But if I paasing the userInfo class to NSNumber, why do I get a timer?

Thanks.

+6
source share
3 answers

Your timer action method should look like this

 -(void)rotateSelectedItem:(NSTimer*)sender 

You can get in userInfo by doing

 NSNumber *userInfo = sender.userInfo; 
+25
source

You misunderstood the selector signature that you register with the timer. The sender of NSTimer* , and not the userInfo object that you pass to its constructor:

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

From the documentation:

Message to send the target when the timer fires. The selector must have the following signature:

 - (void)timerFireMethod:(NSTimer*)theTimer 
+2
source

Source: https://habr.com/ru/post/909774/


All Articles