我有一個自定義UIPickView
,在(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
自定義UIButton
點擊,但不工作,我該怎麼辦?請幫助我們。由於如何添加與UIPickerView單元格的UIButton?
我的代碼:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UIButton *button;
if (!button) {
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, pickerView.bounds.size.width, 44.0);
[button setTitle:_pickDataArray[row] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(pickButtonAction:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 1000 + row;
}
return button;
}
- (void)pickButtonAction:(UIButton *)button{
NSLog(@"button.tag = %ld",(long)button.tag);
}
無需驗證碼
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ }
UPDATA代碼:
UIButton *button;
if (view == nil) {
view = [[UIView alloc]init];
view.backgroundColor = [UIColor clearColor];
view.userInteractionEnabled = YES;
if (!button) {
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, pickerView.bounds.size.width, 44.0);
[button setTitle:_pickDataArray[row] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(pickButtonAction:) forControlEvents:UIControlEventTouchDown];
button.tag = 1000 + row;
[view addSubview:button];
}
}
return view;
- (void)pickButtonAction:(UIButton *)button{
NSLog(@"button.tag = %ld",(long)button.tag);//always not executed
}
self.view.addsubview(按鈕),就把這行中,如果條件按鈕之後。標籤行檢查輸出 –
添加它,但按鈕點擊不起作用 –
- (IBAction)pickButtonAction:(UIButton *)發件人 { NSLog(@「button.tag =%ld」,(long)sen der.tag); }使用像這樣的方法 –