我有一個UIButton
我添加了一個子視圖UIButton
這是一個UILabel
。當我點擊UILabel
時,按鈕的動作不會被觸發。是否有一個簡單的修復方法,以便在觸摸UILabel
時觸摸事件傳遞到UIButton
?
我正在考慮給UILabel
添加手勢識別器,然後觸發UIButton
的操作,不確定是否有更簡單的方法。添加一個UILabel作爲UIButton的子視圖不會觸發按鈕動作
6
A
回答
4
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");
}
相關問題
- 1. UIButton作爲子視圖在UILabel
- 2. 在UITextView上添加一個UILabel作爲子視圖
- 3. 我想要一個按鈕在不同的視圖中觸發一個動作
- 4. 在子視圖中爲按鈕添加動作
- 5. UIButton在子視圖中觸發當前ViewController中的動作
- 6. 如果兩個視圖之間存在UIButton不觸發動作
- 7. 我的按鈕觸發不會工作
- 8. 將按鈕作爲子視圖添加到UIView後不能點擊按鈕
- 9. 添加UISearchBar與範圍按鈕作爲UIView的子視圖
- 10. 在UINavigationBar的添加子視圖的按鈕不起作用
- 11. 曖昧約束添加的UILabel當作爲子視圖的UITextView
- 12. 集合視圖單元格按鈕不觸發動作
- 13. 具有自定義視圖的右導航欄按鈕不會觸發動作
- 14. 添加子視圖的UIButton
- 15. 如何添加uibutton,uiimage作爲mpmovieplayercontroller的子視圖?
- 16. 添加的UIButton到子視圖不工作
- 17. 上的一個按鈕添加一個動作沒有IBAction爲
- 18. 在UIScrollView中添加爲子視圖的UIButton的觸摸檢測
- 19. 自定義UIButton類的子視圖不會觸發點擊
- 20. 如何添加一個按鈕作爲
- 21. 動態添加的按鈕面板,點擊將不會觸發
- 22. 動態添加的鏈接按鈕不會觸發事件
- 23. 按鈕不會添加到視圖
- 24. UIButton在當前視圖添加子視圖後無法工作
- 25. 作爲子視圖添加UIButton遠離UIView
- 26. 將UIButton作爲子視圖添加到UITabBar
- 27. 如何添加一個動作到UIButton
- 28. 添加動作在IOS一個UIButton SDK
- 29. 按鈕會忽略前一個視圖控制器的動作
- 30. 如何在removefromsubview uibutton之後添加按鈕子視圖?
你是如何添加標籤,在代碼或IB?而且,爲什麼要添加一個標籤 - 一個按鈕已經有一個titleLabel,爲什麼要添加另一個? – rdelmar
發佈您用於添加該標籤的代碼 –