与其创建 customView(UIView 的实例),不如将 customView 作为 UIControl 的实例添加,并将 addTarget 添加到 customView 中,
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(10,10,300,300)];
UIControl *customView = [[UIControl alloc] initWithFrame:CGRectMake(0,0,300,300)];
[customView addTarget:self action:@selector(customViewClicked:) forControlEvents:UIControlEventTouchUpInside];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,100)];
[label1 setText:@"Hello How are you ?"];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,150,100,100)];
[label1 setText:@"I am fine Thnank You!"]
[customView addSubView:lebel1];
[customView addSubView:lebel2];
现在在 CustomViewClicked 方法中
-(void)customViewClicked:(id)sender
{
UIControl *senderControl = (UICotrol *)sender;
NSLog(@"sender control = %@",senderControl);
}
希望它会帮助你。