Search code examples
iosuipickerview

UIPickerView not working in sub UIView


I have a UIPickerView that works perfectly when placed on the main view. However as soon as I place it into a sub UIView (To help with visual placement of elements) the interaction stops completely. The UIPickerView still receives all data correctly, just no interaction.

If I take the UIPickerView back out of the UIView, using storyboard, it works right away.

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Add provinces into array
  [self.provinceView setHidden:YES];
  isVisible = NO;
  self.provinceArray = [[NSArray alloc] initWithObjects:@"Alberta",@"British Columbia",@"Manitoba",@"New Brunswick",@"Newfoundland and Labrador",@"Northwest Territories",@"Nova Scotia",@"Nunavut",@"Ontario",@"Prince Edward Island",@"Quebec",@"Saskatchewan",@"Yukon",nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  return [self.provinceArray count];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0,(pickerView.frame.size.width-40), 44)];
  label.textColor = [UIColor colorWithRed:0.035 green:0.094 blue:0.345 alpha:1.0];
  label.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
  label.text = [self.provinceArray objectAtIndex:row];
  return label;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
  self.provinceLabel.text = [self.provinceArray objectAtIndex:row];
}

Solution

  • Check this post --> UIPickerView in UITableView doesn't react to touch in lower half of cell

    did u bring the subview to foreground?