我试图以一种编程的方式添加一个按钮,当按下它时,某个对象就会被传递。我一直收到“未识别的选择器已发送”异常。你能建议一下代码有什么问题吗:
// 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;
}发布于 2010-09-13 15:54:57
在构建NSInvocation时,您不会在任何地方使用它-您只是将selector设置为按钮的action。此选择器应具有类似于- (void)foo:(id)sender, ...的形式。
相反,您可以使用字典,例如,将标签用作映射到某个NSInvocation或存储其他参数的键。
发布于 2010-09-13 19:26:34
我用一种不同的方式解决了它。继承了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然后,再往下看,我执行以下操作:
// 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-13 21:13:16
//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];https://stackoverflow.com/questions/3698462
复制相似问题