0

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

    // 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;
}
4

3 回答 3

1

当您构建 aNSInvocation时,您并没有在任何地方使用它 - 您只是将 设置selectoraction按钮。此选择器应具有类似 - (void)foo:(id)sender, ...的形式。

您可以改为使用带有例如标签的字典作为映射到某个特定NSInvocation或存储附加参数的键。

于 2010-09-13T07:54:57.427 回答
1

我以不同的方式解决了它。子类化 UIButton 并添加了我需要的所有属性。这是类的样子:

@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

Then, further down, I do the following:

    // allocate the details button's action
    RecentSalePropertyDetailsButton* button = (RecentSalePropertyDetailsButton *)[myView viewWithTag:15];
    button.property = property;
    [button addTarget:self action:@selector(showRecentSalesPropertyDetails:) forControlEvents:UIControlEventTouchDown];
于 2010-09-13T11:26:33.693 回答
0
//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];
于 2010-09-13T13:13:16.183 回答