![](/img/trans.png)
[英]UIButton in a subview, trigger action in current ViewController
[英]adding a UILabel as subview of UIButton doesn't trigger button action
我有一個UIButton
,我添加一個子視圖到UIButton
這是一個UILabel
。 當我點擊UILabel
時,按鈕的動作不會被觸發。 是否有一個簡單的解決方案,以便在輕觸UILabel
時將觸摸事件傳遞給UIButton
?
我正在考慮向UILabel
添加手勢識別器,然后觸發UIButton
的動作,不確定是否有更簡單的方法。
您可以禁用用戶與標簽的互動。
_yourLabel.userInteractionEnabled = NO;
嘗試這個,
- (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)];
label.text=@"hai";
label.backgroundColor=[UIColor redColor];
[firstButton addSubview:label];
}
-(void)firstButtonClicked {
NSLog(@"first");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.