带有枚举的 NSInvocation?
NSInvocation with an enum?
------------编辑------------
长话短说:
看来我使用 NSInvocation class 不正确(缺乏指针知识)。
使用它们的方式是这样的(有效):
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];
------------编辑------------
长话短说:
看来我使用 NSInvocation class 不正确(缺乏指针知识)。
使用它们的方式是这样的(有效):
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];