6

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 target is start rotating a view while user holds a button. When user releases it the timer will stop.

But I'm giving this:

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

But if I'm paasing in userInfo a NSNumber class, why I'm receiving the timer?

Thanks.

Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
NemeSys
  • 555
  • 1
  • 10
  • 28

3 Answers3

25

Your timer action method should look like this

-(void)rotateSelectedItem:(NSTimer*)sender

You can get at the userInfo by doing

NSNumber *userInfo = sender.userInfo;
joerick
  • 16,078
  • 4
  • 53
  • 57
2

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];
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

From the documentation:

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

- (void)timerFireMethod:(NSTimer*)theTimer
Arun
  • 3,406
  • 4
  • 30
  • 55
fbernardo
  • 10,016
  • 3
  • 33
  • 46