首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于NSInvocation/invoke对,addTarget失败

对于NSInvocation/invoke对,addTarget失败
EN

Stack Overflow用户
提问于 2015-04-30 16:58:55
回答 1查看 73关注 0票数 0

我从字典值中将目标和操作传递给UIButtons。这很好,除非我尝试传递一个NSInvocation/invoke对。

代码语言:javascript
复制
//   |self| is a member of |MyClass|, which declares selector |test|

NSDictionary *button1Data = @{ @"selectorString" : NSStringFromSelector(@selector(test)),
                               @"target" : self };

[button1 addTarget:button1Data[@"target"]
            action:NSSelectorFromString(button1Data[@"selectorString"])
  forControlEvents:UIControlEventTouchUpInside];

//  button1 executes target fine

NSInvocation *testInvocation = [NSInvocation invocationWithMethodSignature:[MyClass instanceMethodSignatureForSelector:@selector(test)]];
    testInvocation.selector = @selector(test);
    testInvocation.target = self;
    [testInvocation retainArguments];

NSDictionary *button2Data = @{ @"selectorString" : NSStringFromSelector(@selector(invoke)),
                                       @"target" : testInvocation };

[button2 addTarget:button2Data[@"target"]
            action:NSSelectorFromString(button2Data[@"selectorString"])
  forControlEvents:UIControlEventTouchUpInside];

//  button2 executes gives exceptions

button2的异常没有寄存器转储。

我在看什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-01 01:19:34

addTarget:action:forControlEvents:不保留目标。您的NSInvocation对象是在函数内部本地创建的,当没有人对它有强烈的引用时,它将在函数的末尾被释放。然后,该按钮将向已释放的实例发送一条消息,从而导致各种不好的事情。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29973644

复制
相关文章

相似问题

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