UIB кнопка с NSInvocation - PullRequest
       7

UIB кнопка с NSInvocation

0 голосов
/ 13 сентября 2010

Я пытаюсь добавить кнопку программным способом, чтобы при нажатии на нее передавался определенный объект.Я продолжаю получать исключение «нераспознанный селектор отправлен».Можете ли вы предложить что не так с кодом:

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

Ответы [ 3 ]

1 голос
/ 13 сентября 2010

Я решил это по-другому. Подкласс 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];
1 голос
/ 13 сентября 2010

Пока вы строите NSInvocation, вы его нигде не используете - вы просто устанавливаете selector в качестве action для кнопки. Ожидается, что этот селектор будет иметь форму, подобную - (void)foo:(id)sender, ... .

Вместо этого вы можете использовать словарь, например, тег как ключ, который отображается на определенный NSInvocation или хранит дополнительные аргументы.

0 голосов
/ 13 сентября 2010
//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];
...