首页 > iOS 自定义UIPickerView的view为一个button,button无法点击

iOS 自定义UIPickerView的view为一个button,button无法点击

如题,今天遇到一个很奇怪的问题,我直接在故事版里拖了一个pickerView,然后实现代理方法

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 10;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UIButton *button = [[UIButton alloc] init];
    [button addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = row;
    [button setTitle:[NSString stringWithFormat:@"%@-%@",@(component),@(row)] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor redColor];
    [button setTintColor:[UIColor redColor]];
    return button;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40;
}

- (void)btnclick:(UIButton *)sender {
    NSLog(@"%ld",(long)sender.tag);
}

就上面这样,直接定义pickerView每一行都是一个button,这样是可以显示出来,但是根本没法响应到按钮的点击事件,也不崩溃不报错,不知道是什么原因,求解


你好 ,我也遇到了同一个问题,你是怎么解决的啊


我写过一篇关于UIButton无法点击的分析博客,你可以看看,希望对你有帮助:http://www.hcios.com/archives/1066

【热门文章】
【热门文章】