iOSで、プログラムでボタンを作成しましたが、イベントが発生しません。
UIButton * anyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[anyButton addTarget:self action:@selector(handleAnyButton:) forControlEvents:UIControlEventTouchUpInside];
anyButton.frame = CGRectMake(150, 350, 22, 22);
[self addSubview:anyButton];
コードはカスタムビュー(カスタムビューコントローラーではない)内に配置されますが、イベントを発生させることはできません。プレスアニメーションが表示されていません。カスタムビューでuserInteractionEnabledが有効になっています。また、ストーリーボードを使用して同じビューに別のボタンを作成してみましたが、そのボタンは機能しています。ボタンコードはinitWithFrameで行われます。私が捕まえなかったいくつかの単純なエラーであるに違いありません、そして私は何時間もこれに頭を打ちつけてきました。
編集:
イベントハンドラ:
-(void) handleAnyButton:(UIButton *)button
{
NSLog(@"handleAnybutton called");
}
EDIT2:上記のボタンコードは、クラスPKLocationsSelectionViewの[自己設定]内で実行されます。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
return self;
}
そして、このビューは、この階層を担当するビューコントローラー(PKLocSelectionViewController)内でプログラムによって作成されました。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
CGFloat fieldsHeight = 88;
UIView * view = [[PKLocationsSelectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, fieldsHeight)];
[self.view addSubview:view];
}