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;
}
.
사용자가 버튼을 보유하고있는 동안 대상이보기를 돌리기 시작합니다.사용자가 해제하면 타이머가 멈 춥니 다.
그러나 나는 이것을주고있다 :
- [__ nscftimer 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