UIButton 与 NSInspiration

发布于 2024-09-19 02:23:46 字数 893 浏览 3 评论 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;
}

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恏ㄋ傷疤忘ㄋ疼 2024-09-26 02:23:46

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

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

While you build a NSInvocation, you are not using it anywhere - you are just setting the selector as the action 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.

三岁铭 2024-09-26 02:23:46

我用不同的方式解决了它。对 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];

I solved it in a different way. Subclassed UIButton and added all the properties I needed to. This is how the class looks like:

@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];
┾廆蒐ゝ 2024-09-26 02:23:46
//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];
//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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文