gpt4 book ai didi

ios - 如何使用 UIPickerView 单元添加 UIButton?

转载 作者:行者123 更新时间:2023-12-01 19:56:12 25 4
gpt4 key购买 nike

我有一个自定义 UIPickView ,在 (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view定制 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{ }

更新代码:
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
}

最佳答案

- (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 并将按钮添加到您的 View 。

关于ios - 如何使用 UIPickerView 单元添加 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42619388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com