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");
}
私の問題はタップです:メソッドが呼び出されません。また、通常のUIButtonで試してみましたが、機能しました。カスタムxibファイルに問題があるはずです。最初にタップアクションで応答するビューがわかりませんか?タップアクションで応答するボタンに設定するにはどうすればよいですか?
私は何を逃しましたか?
ここで完全なxcodeプロジェクトを見つけることができます:
https ://www.dropbox.com/s/zjhwy7jm8jcywz4/blabla1_111.zip
ありがとうございました!