2012-09-05 55 views
6

我有一个UIButton我添加了一个子视图UIButton这是一个UILabel。当我点击UILabel时,按钮的动作不会被触发。是否有一个简单的修复方法,以便在触摸UILabel时触摸事件传递到UIButton
我正在考虑给UILabel添加手势识别器,然后触发UIButton的操作,不确定是否有更简单的方法。添加一个UILabel作为UIButton的子视图不会触发按钮动作

+1

你是如何添加标签,在代码或IB?而且,为什么要添加一个标签 - 一个按钮已经有一个titleLabel,为什么要添加另一个? – rdelmar

+0

发布您用于添加该标签的代码 –

回答

4

您可能只是禁用用户与您的标签交互。

_yourLabel.userInteractionEnabled = NO; 
+2

不,这不起作用 – adit

+0

为我工作.. – Esqarrouth

4

试试这个,

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UIButton *firstButton=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [firstButton setTitle:@"first" forState:UIControlStateNormal]; 
    firstButton.frame=CGRectMake(40, 70, 80, 80); 
    [firstButton addTarget:self action:@selector(firstButtonClicked) forControlEvents: UIControlEventTouchUpInside]; 
    [self.view addSubview:firstButton]; 

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 80)]; 
    [email protected]"hai"; 
    label.backgroundColor=[UIColor redColor]; 
    [firstButton addSubview:label]; 

} 

-(void)firstButtonClicked { 
    NSLog(@"first"); 
} 
相关问题