我有一个加载 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
谢谢!