简体   繁体   English

NSTimer userInfo。 对象如何传递给选择器?

[英]NSTimer userInfo. How object is passing to the 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 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 - [__ NSCFTimer intValue]:无法识别的选择器发送到实例0x4ae360

But if I'm paasing in userInfo a NSNumber class, why I'm receiving the timer? 但是,如果我在userInfo中调用一个NSNumber类,为什么我要收到定时器?

Thanks. 谢谢。

Your timer action method should look like this 您的计时器操作方法应如下所示

-(void)rotateSelectedItem:(NSTimer*)sender

You can get at the userInfo by doing 您可以通过执行来获取userInfo

NSNumber *userInfo = sender.userInfo;

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: 发件人是NSTimer* ,而不是传递给其构造函数的userInfo对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM