私はいくつかのラベルとボタンを作成しているクラスCustomView
( UIView
xib ファイルなしのサブクラス) を持っています。このクラスを別のクラスで使用しUIViewController
て、それらのラベルとボタンを追加したかったのです。カスタムビューを使用してviewControllerにラベルとボタンを追加できますが、ボタン(カスタムビューにある)にアクションまたはイベントを追加すると機能しません。ボタンのアクションを追加するにはどうすればよいか教えてください。
//ViewController code
CustomView *slider=[[CustomView alloc]init];
[self.view addSubview:slider];
//CustomView code
toggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[toggleButton setTitle:@"" forState:UIControlStateNormal];
toggleButton.userInteractionEnabled=YES;
// add drag listener
[toggleButton addTarget:self action:@selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
// center and size
toggleButton.frame = CGRectMake(frame.origin.x, frame.origin.y, width, frame.size.height);
toggleButton.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.0 alpha:0.1];
[toggleButton.layer setBorderWidth:4.0];
[toggleButton.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
toggleButton.layer.cornerRadius=4.0;
[toggleButton setTitleColor:[UIColor colorWithRed:0.3 green:0.1 blue:0.4 alpha:1.0] forState:UIControlStateNormal];
// add it, centered
[self addSubview:toggleButton];
- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
NSLog(@"inside drag");
}