首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带枚举的NSInvocation?

带枚举的NSInvocation?
EN

Stack Overflow用户
提问于 2016-08-25 15:40:35
回答 1查看 166关注 0票数 0

TL;DR:

我似乎错误地使用了NSInvocation类(缺乏指针知识)。

使用这些方法的方式如下(工作):

代码语言:javascript
复制
NSString *str = @"string";
UIControlState state = UIControlStateNormal;
[invocation setArgument: &str atIndex: 2];
[invocation setArgument: &state atIndex: 3];

是否有可能使用枚举参数进行setArgument:atIndex:

请参见以下示例(不起作用):

代码语言:javascript
复制
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:...中运行得很好。

代码语言:javascript
复制
[btn performSelector: selector withObject: @"Testing" withObject:@(UIControlStateNormal)];

我不确定我是否正确地使用了NSInovcation。有人想详细说明吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-10 03:18:28

试试这个:

代码语言:javascript
复制
NSString *test = @"Test";
[invocation setArgument: &test atIndex: 2];

UIControlState state = UIControlStateNormal;
[invocation setArgument: &state atIndex: 3];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39149483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档