0

ボタンコンストラクタークラスがあり、そのクラスにはボタンを設計するためのこのメソッドがあります

- (void)setupWithPosition:(CGPoint)point withImage:(UIImage *)image withImageFloat:(NSString *)imageFloat withTitle:(NSString *)title withLineWidth:(CGFloat)lineWidth respondsToSelector:(NSString*)selector
{
    // create button
    ButtonWithImage* button = [[ButtonWithImage alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];

    //[button setTitle:[title uppercaseString] forState:UIControlStateNormal];
    [button setupViewWithText:title andImage:image andImageFloat:imageFloat];


    [button addTarget:self
               action:NSSelectorFromString(selector)
     forControlEvents:UIControlEventTouchUpInside];

    CGRect buttonFrame = button.frame;
    CGFloat lineX = 0.0f;


    // setup image
    if ([[imageFloat lowercaseString] isEqualToString:@"right"]) {
        lineX = buttonFrame.origin.x + buttonFrame.size.width;
    } else {
        lineX = 0.0f;
        buttonFrame.origin.x = lineWidth;
        button.frame = buttonFrame;
    }

    self.line = [[UIView alloc] initWithFrame:CGRectMake(lineX, buttonFrame.size.height/2, lineWidth, LINE_HEIGHT)];
    self.line.backgroundColor = [UIColor lineNormalColor];


    [self addSubview:self.line];
    [self addSubview:button];
}

そして、私はそれを使用して呼び出しています:

homepageButtons = [[NSMutableArray alloc] init];

    CategoryButtonView* za = [[CategoryButtonView alloc] initWithFrame:CGRectMake(200, 250, 300, 46)];
    [za setupWithPosition:CGPointMake(100, 100) withImage:[UIImage imageNamed:@"zas.png"] withImageFloat:@"right" withTitle:@"za" withLineWidth:80.0f respondsToSelector:@"buttonClicked"];

ボタンは正しく配置されていますが、ボタンをクリックするとエラーが発生します

CategoryButtonView buttonClicked]: unrecognized selector sent to instance 0xa1a46a0
2012-11-02 17:53:43.844 Man[3432:14c03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CategoryButtonView buttonClicked]: unrecognized selector sent to instance 0xa1a46a0'

SELの問題だと思いますが、どうすれば解決できますか?

4

2 に答える 2

3

次の行があります。

[button addTarget:self
           action:NSSelectorFromString(selector)
 forControlEvents:UIControlEventTouchUpInside];

ここで「自分」が本当に欲しいですか?つまり、渡すセレクターは、CategoryButtonViewクラスのメソッドである必要があり、CategoryButtonView.

ほとんどの場合、targetパラメーターと共にパラメーターを追加する必要がありselectorます。

于 2012-11-02T16:13:55.730 に答える
0

それを機能させるためにそれを追加する必要がありました

NSString *temp = [NSString stringWithFormat:@"%@:",selector];

    [button addTarget:sender
               action:NSSelectorFromString(temp)
     forControlEvents:UIControlEventTouchUpInside];
于 2012-11-02T16:43:22.717 に答える