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

を作成している間はNSInvocation、どこでも使用していません。 をボタンのselectorとして設定しているだけです。このセレクターは、...actionのような形式であることが期待され ます。- (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

次に、さらに下に、次のことを行います。

    // 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 に答える