繁体   English   中英

UIPickerview 中的 UIButton 没有响应

[英]UIButton in UIPickerview is not responding

我在 UIPickerview 中有一个自定义视图,如下所示

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
  UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 30)];
  UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 30)];
  [btn addTarget:self action:@selector(filtercategory) forControlEvents:UIControlEventTouchUpInside];
  btn.showsTouchWhenHighlighted = YES;
  [btn setImage:[UIImage imageNamed:@"longmp"] forState:UIControlStateNormal];
  [vw addSubview:btn];
  return vw;
}

方法filtercategory没有被调用。 我搜索了解决方案。 但是拿不到。

甚至showsTouchWhenHighlighted也不起作用。

选择器视图看起来像

在此处输入图片说明

添加了 UiButton,因为是纯白色图像,我尝试了不同的图像来确认。

如何让 UIButton 响应?

尝试

[btn setUserInteractionEnabled:YES];

编辑

如果您希望用户在 UIPickerView 中滚动到该项目后选择当前项目,那么UIActionSheet可以帮助您。 可以帮助您在UIPickerView顶部添加UIToolBar

您只需要在 UIToolBar 中添加 UIBarButtonItem ,它对 iOS 用户来说看起来会更自然。

确保两件事:

1)它没有出现在这个委托函数中,因为你想处理你的按钮点击:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component

2)我对这里的按钮框架有疑问:

[[UIButton alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 30)];

确保按钮位于控件的活动区域中。 我的经验是,如果控件超出父控件的范围,它不会接收触摸事件。

3)尝试使用UITapGestureRecognizer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM