【问题标题】:UIButton with custom view - addTarget:action:forControlEvents: does not work具有自定义视图的 UIButton - addTarget:action:forControlEvents: 不起作用
【发布时间】:2013-05-08 13:53:57
【问题描述】:

我的按钮如下

  1. 已创建标签1
  2. 已创建标签2
  3. 创建自定义视图 (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
{
}

但即使我触摸按钮,也不会调用上述函数。有什么解决办法吗?

【问题讨论】:

  • 请把您的相关代码放在这里?

标签: iphone ios uibutton uitouch


【解决方案1】:

朋友,你的代码只是一个小问题,你只需要在你的代码中添加以下一行,忘记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__);
}

【讨论】:

  • 你确定视图的setUserInteractionEnabled 应该是NO吗?我将它设置为是,然后它起作用了!
  • 是的,因为在self.view 中添加了按钮,而在view 中添加了标签,这就是为什么如果你禁用它的用户交互。它没有任何区别,因为按钮和标签位于不同的容器中。
【解决方案2】:

而不是创建 customView(UIView 的实例),您将 customView 作为 UIControl 的实例添加为 customView 的 addTarget,

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);
}

希望对你有帮助。

【讨论】:

    【解决方案3】:

    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 的用户交互,因为它被添加到顶部,最终会干扰我们想要捕捉的点击手势。

    为了更清楚地看到这一点,只需在调试时使用“Debug View Hierarchy”即可:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多