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 回答