nstimer userinfo。对象如何传递给选择器?
题
我有这个代码:
-(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;
}
.
目标是在用户保存按钮时旋转视图。当用户释放它时,计时器将停止。
但我给出这个:
- [__ nscfterimer intvalue]:无法识别选择器发送到实例0x4ae360
但如果我在UserInfo中保存了一个NSNumber类,为什么我收到定时器?
谢谢。
解决方案
Your timer action method should look like this
-(void)rotateSelectedItem:(NSTimer*)sender
You can get at the userInfo by doing
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:
-(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
不隶属于 StackOverflow