我有一个子类UIButton,它加载了一个Nib:
@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");
}
我的问题是点击方法不会被调用。我已经尝试过使用普通的UIButton,它可以正常工作,因此问题肯定出在自定义xib文件上。但我不知道哪个视图首先响应了点击事件?我该如何设置按钮为首要响应点击事件?
我错过了什么?
您可以在这里找到完整的Xcode项目:
https://www.dropbox.com/s/zjhwy7jm8jcywz4/blabla1_111.zip 谢谢!