首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NSInvocation的UIButton

使用NSInvocation的UIButton
EN

Stack Overflow用户
提问于 2010-09-13 15:18:25
回答 3查看 857关注 0票数 0

我试图以一种编程的方式添加一个按钮,当按下它时,某个对象就会被传递。我一直收到“未识别的选择器已发送”异常。你能建议一下代码有什么问题吗:

代码语言:javascript
复制
    // 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];

再往下看,同一个类中的方法如下所示:

代码语言:javascript
复制
-(void) showPropertyDetails:(id)something forProperty:(Property *)property {
int i=0;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-09-13 15:54:57

在构建NSInvocation时,您不会在任何地方使用它-您只是将selector设置为按钮的action。此选择器应具有类似于- (void)foo:(id)sender, ...的形式。

相反,您可以使用字典,例如,将标签用作映射到某个NSInvocation或存储其他参数的键。

票数 1
EN

Stack Overflow用户

发布于 2010-09-13 19:26:34

我用一种不同的方式解决了它。继承了UIButton,并添加了我需要的所有属性。这是这个类的样子:

代码语言:javascript
复制
@interface RecentSalePropertyDetailsButton : UIButton {
    Property* property;
}
@property (nonatomic, retain) Property* property;
@end
@implementation RecentSalePropertyDetailsButton
@synthesize property;
- (id) initWithPropertyAs:(Property*)aProperty{
    [super init];
    self.property = aProperty;
    return self;
}
@end

然后,再往下看,我执行以下操作:

代码语言:javascript
复制
    // allocate the details button's action
    RecentSalePropertyDetailsButton* button = (RecentSalePropertyDetailsButton *)[myView viewWithTag:15];
    button.property = property;
    [button addTarget:self action:@selector(showRecentSalesPropertyDetails:) forControlEvents:UIControlEventTouchDown];
票数 1
EN

Stack Overflow用户

发布于 2010-09-13 21:13:16

代码语言:javascript
复制
//make a button
    UIButton *button = [UIButton buttonWithType:0];
//set button size
    button.frame = CGRectMake(20,219,280,106);
//give it a color
    button.backgroundColor = [UIColor clearColor];
//add a method
    [button addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
//add it to the currentview 
    [self.view addSubview:button];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3698462

复制
相关文章

相似问题

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