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。有人想详细说明吗?
发布于 2016-10-10 03:18:28
试试这个:
NSString *test = @"Test";
[invocation setArgument: &test atIndex: 2];
UIControlState state = UIControlStateNormal;
[invocation setArgument: &state atIndex: 3];https://stackoverflow.com/questions/39149483
复制相似问题