UIButton 与 NSInspiration
我试图以编程方式添加一个按钮,按下按钮后,某个对象就会被传递。我不断收到“发送无法识别的选择器”异常。您能否建议代码有什么问题:
// allocate the details button's action
SEL selector = @selector(showPropertyDetails:forProperty:);
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
//The invocation object must retain its arguments
[property retain];
//Set the arguments
[invocation setTarget:self];
[invocation setArgument:&property atIndex:3];
[(UIButton*)[myView viewWithTag:15] addTarget:self action:@selector(selector) forControlEvents:UIControlEventTouchDown];
再往下看,同一个类中的方法如下所示:
-(void) showPropertyDetails:(id)something forProperty:(Property *)property {
int i=0;
}
I am trying to add a button programatically in a way that upon pressing it, a certain object is being passed. I keep on getting "unrecognized selector sent" exception. Can you suggest whats wrong with the code:
// allocate the details button's action
SEL selector = @selector(showPropertyDetails:forProperty:);
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
//The invocation object must retain its arguments
[property retain];
//Set the arguments
[invocation setTarget:self];
[invocation setArgument:&property atIndex:3];
[(UIButton*)[myView viewWithTag:15] addTarget:self action:@selector(selector) forControlEvents:UIControlEventTouchDown];
and further down, the method in the same class looks like:
-(void) showPropertyDetails:(id)something forProperty:(Property *)property {
int i=0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您构建
NSInspiration
时,您不会在任何地方使用它 - 您只是将selector
设置为按钮的action
。该选择器的形式应类似于- (void)foo:(id)sender
, ...。您可以改为使用字典,例如将标签作为映射到某个
NSIn Vocation
或存储其他参数的键。While you build a
NSInvocation
, you are not using it anywhere - you are just setting theselector
as theaction
for the button. This selector is expected to have a form like- (void)foo:(id)sender
, ....You could instead use a dictionary with e.g. the tag as a key that maps to a certain
NSInvocation
or stores additional arguments.我用不同的方式解决了它。对 UIButton 进行子类化并添加我需要的所有属性。这就是类的样子:
然后,再往下,我执行以下操作:
I solved it in a different way. Subclassed UIButton and added all the properties I needed to. This is how the class looks like:
Then, further down, I do the following: