Search code examples
iosios7uibuttonuipickerview

UIButton addTarget doesn't work with iOS7,but works in IOS6


Obviously, this code works in iOS 6. By the way, it has problem in iOS 7.

I add UIButton to every row in a UIPickerView:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UIButton *btnRow=[UIButton buttonWithType:UIButtonTypeSystem];
    [btnRow addTarget:self action:@selector(touch:) forControlEvents:UIControlEventTouchUpInside];
    btnRow.frame=CGRectMake(0, 0, 320, 30);
    int tag= selfTextField.tag/10000 - 1;
    [btnRow setTitle:[[pickerArray objectAtIndex:tag] objectAtIndex:row] forState:UIControlStateNormal];

    return btnRow;
}

Solution

  • You can't place a button on picker view row. So i have added a toolbar above UIPickerView and added a bar button item on it. I use the row number to perform desired functionality through selector

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
         ....
         //get the row number & Perform desired functionality
         ....
    }
    

    you will get the row number and use that to perform desired functionality.