簡體   English   中英

具有自定義視圖的UIButton-addTarget:action:forControlEvents:不起作用

[英]UIButton with custom view - addTarget:action:forControlEvents: does not work

我的按鈕如下

  1. 創建的標簽1
  2. 創建的標簽2
  3. 創建了customView( UIView
  4. 在自定義視圖上添加了label1和label2
  5. 創建了myCustomButton( UIButton
  6. 在myCustomButton上添加了customView

我已經為custom_View,label1和label2完成了userInteractionEnable。

然后添加

[myCustomButton addTarget:self action:@selector(OnButtonClick:) forControlEvents:UIControlEventTouchUpInside];

-(void)OnButtonClick:(UIButton *)sender
{
}

但是,即使我觸摸按鈕,也永遠不會調用上述功能。 有什么辦法嗎?

我的朋友,您的代碼只是一個小問題,您只需要在代碼中添加以下一行,而忘記將setUserInteractionEnabled:NOUIView ,它將允許您單擊按鈕

UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[lbl1 setText:@"ONe"];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 100, 30)];
[lbl2 setText:@"Two"];

UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 130)];
[view setUserInteractionEnabled:NO];

[view addSubview:lbl1];
[view addSubview:lbl2];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addSubview:view];
[btn setFrame:CGRectMake(0, 0, 200, 130)];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

點擊方式

-(void)click
{
    NSLog(@"%s",__FUNCTION__);
}

無需創建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);
}

希望對您有幫助。

Swift 4.2解決方案

這是Swift的最新版本的問題的解決方案(基於先前的答案):

func customButton() {

    // Label Creation
    let firstLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
    firstLabel.text = "first"

    let secondLabel = UILabel(frame: CGRect(x: 0, y: 30, width: 100, height: 30))
    secondLabel.text = "second"

    // Custom View creation
    let viewFrame = CGRect(x: 0, y: 0, width: 100, height: 60)
    let customView = UIView(frame: viewFrame)
    customView.addSubview(firstLabel)
    customView.addSubview(secondLabel)
    customView.isUserInteractionEnabled = false

    // Custom button
    let button = UIButton(type: .custom)
    button.frame = viewFrame
    button.addSubview(customView)

    button.addTarget(self, action: #selector(click), for: .touchUpInside)
    addSubview(button)
}

@objc
func click() {
    print("Click")
}

注意 :絕對需要禁用customView的用戶交互,因為它是添加在頂部的,最終將干擾我們要捕獲的輕customView手勢。

為了更清楚地看到這一點,在調試時只需使用“ Debug View Hierarchy”:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM