gpt4 book ai didi

iphone - 带有自定义 View 的 UIButton - addTarget :action:forControlEvents: does not work

转载 作者:可可西里 更新时间:2023-11-01 03:23:46 25 4
gpt4 key购买 nike

我的按钮如下

  1. 创建标签 1
  2. 创建标签 2
  3. 创建自定义 View (UIView)
  4. 在自定义 View 上添加了 label1 和 label2
  5. 创建了 myCustomButton(UIButton)
  6. 在 myCustomButton 上添加了自定义 View

我已经为 custom_View、label1 和 label2 完成了 userInteractionEnable。

然后添加

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

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

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

最佳答案

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

关于iphone - 带有自定义 View 的 UIButton - addTarget :action:forControlEvents: does not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16442559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com