4

我有一个加载 Nib 的子类 UIButton:

@implementation BlaButtonVC

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSArray *subViews = [[NSBundle mainBundle] loadNibNamed:@"BlaButton" owner:self options:nil];
        [self addSubview:subViews[0]];
    }
    return self;
}

@end

我可以将此按钮添加到视图中,如下所示:

// ViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    BlaButtonVC *button = [[BlaButtonVC alloc] initWithFrame:CGRectMake(10, 10, 280, 44)];

    button.titleXLabel.text = @"Bls";
    button.descLabel.text = @"Kues";

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

    [button becomeFirstResponder];

    [self.view addSubview:button];
}

-(IBAction)tap:(id)sender
{
    NSLog(@"sssss");
}

我的问题是 tap: 方法不会被调用。还尝试了一个普通的 UIButton,它确实有效,所以它一定是自定义 xib 文件的问题,我真的不知道哪个视图首先响应点击动作?如何将按钮设置为响应点击操作的按钮?

我错过了什么?

在这里您可以找到完整的 xcode 项目:
https ://www.dropbox.com/s/zjhwy7jm8jcywz4/blabla1_111.zip

谢谢!

4

1 回答 1

2

禁用视图的子视图,因为它们会触动。
在 xib ( userinteraction enabled) 或代码中:

((UIView *)subViews[0]).userInteractionEnabled = NO;

于 2012-12-09T13:20:34.037 回答