代码之家  ›  专栏  ›  技术社区  ›  Amarsh

带NSInvocation的UI按钮

  •  0
  • Amarsh  · 技术社区  · 14 年前

    我试图添加一个按钮的编程方式,当按下它,一个特定的对象正在通过。我不断收到“未识别的选择器发送”异常。你能告诉我代码有什么问题吗

        // 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 回复  |  直到 14 年前
        1
  •  1
  •   Georg Fritzsche    14 年前

    NSInvocation selector 作为 action 为了按钮。此选择器应具有如下形式 - (void)foo:(id)sender , ... .

    或存储其他参数。

        2
  •  1
  •   Amarsh    14 年前

    我用另一种方法解决了。子类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];
    
        3
  •  0
  •   ssj    14 年前
    //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];