2012-09-05 53 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"); 
} 
相關問題