[英]NSInvocation with an enum?
------------编辑-------------
TL; DR:
看来我错误地使用了NSInvocation类(缺少指针知识)。
使用这些方法的方式如下所示(工作):
NSString *str = @"string";
UIControlState state = UIControlStateNormal;
[invocation setArgument: &str atIndex: 2];
[invocation setArgument: &state atIndex: 3];
---------------原始问题---------
可以用枚举参数设置setArgument:atIndex:
吗?
请参见以下示例(无效):
UIButton *btn = ...;
SEL selector = @selector(setTitle:forState:);
NSInovcation *invocation = [NSInovcation invocationWithMethodSignature: [btn methodSignatureForSelector: selector]];
[invocation setSelector: selector];
[invocation setArgument: @"Test" atIndex: 2];
//Nothing happens
UIControlState state = UIControlStateNormal;
[invocation setArgument: &state atIndex: 3];
//Runtime error (the pointer is NULL)
UIControlState *state = UIControlStateNormal;
[invocation setArgument: state atIndex: 3];
//No errors but no effect
int state2 = 0;
[invocation setArgument: &state atIndex: 3];
//Compile error (casting)
[invocation setArgument: @(UIControlStateNormal) atIndex: 3];
//Runtime EXC_BAD_ACCESS
[invocation setArgument: (void *)@(UIControlStateNormal) atIndex: 3];
...
[invocation invokeWithTarget: btn];
奇怪的是(或不是),它与performSelector:...
一起使用时效果很好:
[btn performSelector: selector withObject: @"Testing" withObject:@(UIControlStateNormal)];
我不确定我是否以正确的方式使用NSInovcation。 有人想详细说明吗?
尝试这个:
NSString *test = @"Test";
[invocation setArgument: &test atIndex: 2];
UIControlState state = UIControlStateNormal;
[invocation setArgument: &state atIndex: 3];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.