-2

I have a custom view for row in UIPickerview as follow

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

The method filtercategory didn't get called. I searched for the solution. But can't get.

Even the showsTouchWhenHighlighted is not working.

The pickerview looks like

enter image description here

UiButton got added, since this is the plain white image, I tried different images to confirm.

How to make the UIButton to respond?

Nazik
  • 8,696
  • 27
  • 77
  • 123
  • Any suggestions would be appreciated. – Nazik Apr 04 '14 at 11:23
  • @CW0007007, since the requirement is the user has to click the row after selecting the row. – Nazik Apr 04 '14 at 12:00
  • Why though, this doesn't make sense to me. A picker view is just that, you pick the value and something happens. You don't pick a row then tap a button!!!! What if they miss tap and the row then scrolls to another as it should... You may want to rethink your approach in my opinion. – CW0007007 Apr 04 '14 at 13:07

2 Answers2

1

Try

[btn setUserInteractionEnabled:YES];

Edit

If you want user to select current item after scrolling to that item in UIPickerView then UIActionSheet can help you. This can help you to add UIToolBar at top of UIPickerView.

You only need to add UIBarButtonItem in UIToolBar and It will look more natural to iOS users.

Community
  • 1
  • 1
Faisal Ali
  • 1,135
  • 10
  • 17
1

Make sure of two things:

1) It is not coming in this delegate function, since you want to handle your button click instead:

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

2) I have doubt on the button's frame here:

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

Make sure the button resides in the active area of the control. I have experience that if a control is outside the bounds of parent control it do not receive touches events.

3) Try with UITapGestureRecognizer.

Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • since the requirement is the user has to click the row after selecting the row. I can't use the didSelectRow method. and the button is come under view's frame. I can see the button's image within the view for row. – Nazik Apr 04 '14 at 12:03
  • for the time being change the color of both button and uiview and don;t set image, then see if everything appears ant its position ? – NeverHopeless Apr 04 '14 at 12:05