0

我有这个代码:

[self performSelector:@selector(animationWithType:) withObject:PasscodeAnimationTypeConfirm afterDelay:0.2];

对这个方法:

-(void)animationWithType:(PasscodeAnimationType)type;

把它放在它的位置:

[self performSelector:@selector(animationWithType:) withObject:[NSNumber numberWithInt:PasscodeAnimationTypeConfirm] afterDelay:0.2];

返回“1”的 NSLog,我的方法没有将其归类为与 PasscodeAnimationTypeConfirm 相同的值。我怎样才能解决这个问题?

4

2 回答 2

2

是的,据我所知,您只能使用对象作为参数而不是类型(整数、字符等)来执行 performSelector:blahBlah:withDelay:。

您可以创建另一个函数来帮助您,如下所示:

-(void)animationWithNumber:(NSNumber)type{
    [self animationWithType:[NSNumber intValue]];
}

并使用您发布的代码中的这个:

[self performSelector:@selector(animationWithType:) withObject:[NSNumber numberWithInt:PasscodeAnimationTypeConfirm] afterDelay:0.2];
于 2011-07-30T15:34:03.247 回答
1

@Emilio:您的 performSelector 调用不应该调用您的新方法吗?

[self performSelector:@selector(animationWithNumber:) withObject:[NSNumber numberWithInt:PasscodeAnimationTypeConfirm] afterDelay:0.2];

这是我的一个例子,展示了这个概念的更详细用法。

http://kerkermeister.net/objective-c-adapter-from-nsinteger-to-id-when-using-performselector-withobject/

适配器看起来像这样并执行更多检查:

-(void)animationWithNumber:(id)_animationType {
    if ([_animationType respondsToSelector:@selector(intValue)]) {
        int _t = [_animationType intValue];
        switch (_t) {
            case AnimationType1:
                _t = AnimationType1;
                break;
            case AnimationType2:
                _t = AnimationType2;
                break;
            default:
                _t = AnimationTypeUndefined;
                break;
        }
        [self animationWithType:_t];
    }
}
于 2013-09-17T19:08:18.397 回答