2017-03-06 37 views
1

我有一個自定義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 
} 
+0

self.view.addsubview(按鈕),就把這行中,如果條件按鈕之後。標籤行檢查輸出 –

+0

添加它,但按鈕點擊不起作用 –

+0

- (IBAction)pickButtonAction:(UIButton *)發件人 { NSLog(@「button.tag =%ld」,(long)sen der.tag); }使用像這樣的方法 –

回答

0

OK,我明白了,按鈕的Click事件塊與UIPickTableViewWrapperCell

enter image description here

我的代碼:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ 
    id view = [super hitTest:point withEvent:event]; 
    if ([view isKindOfClass:[UIView class]]) { 
     return nil; 
    } 
    return [view hitTest:[self convertPoint:point toView:view] withEvent:event]; 
} 
1
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { 
    if (view == nil) { 
     view = [[UIView alloc] init]; 
     view.backgroundColor = [UIColor blueColor]; 
     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; 
} 

你只是忘記返回UIView並添加按鈕到您的視圖。

+0

此視圖爲空=。= –

+0

檢查我更新的答案。 –

+0

'pickButtonAction:'總是不執行 –

相關問題