标签上显示的 UIPickerView 选项
Posted
技术标签:
【中文标题】标签上显示的 UIPickerView 选项【英文标题】:UIPickerView choices shown on label 【发布时间】:2014-10-13 03:11:35 【问题描述】:我的 UIPickerView 有 3 个组件,下面有 3 个标签。当为选择器的一个组件选择答案时,我希望第一个标签显示该组件的答案。然后第二个和第三个组件显示在第二个和第三个标签上。但是它目前没有像我想的那样工作,有什么建议吗?
我认为我的问题在于“-(void)pickerView:(UIPickerView *)pickerView didSelectRow:”部分
#import "ViewController.h"
@interface ViewController2 : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *picker1;
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *label3;
@end
和:
import "ViewController2.h"
@interface ViewController2 ()
NSArray*othercolourArray;
NSArray*otherseasonArray;
NSArray*otherotherArray;
@end
@implementation ViewController2
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
othercolourArray =[[NSArray alloc]initWithObjects:@"Health Prof", @"Housekeep", @"Visitors", @"Other",nil];
otherseasonArray =[[NSArray alloc]initWithObjects:@"Midwife", @"Nurse", @"Doctor",@"Other", nil];
otherotherArray =[[NSArray alloc]initWithObjects:@"Known Midwife", @"New Midwife", @"Doctor",@"Other", nil];
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
return 3;
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
switch (component)
case 0:
return othercolourArray.count;
break;
case 1:
return otherseasonArray.count;
break;
case 2:
return otherotherArray.count;
break;
default:
break;
return 0;
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
// This method provides the data for a specific row in a specific component.
switch (component)
case 0:
return [othercolourArray objectAtIndex:row];
break;
case 1:
return [otherseasonArray objectAtIndex:row];
break;
case 2:
return [otherotherArray objectAtIndex:row];
break;
default:
break;
return 0;
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
_label1.text = [othercolourArray objectAtIndex: row];
_label2.text = [otherseasonArray objectAtIndex: row ];
_label3.text = [otherotherArray objectAtIndex: row ];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
@end
【问题讨论】:
However it is currently not working as I thought, any suggestions?
请详细说明或给我们错误日志之类的:(
【参考方案1】:
我认为您对pickerView:didSelectRow:inComponent
的实现需要添加更多逻辑。目前,它看起来会根据最后一个组件行组合来切换所有标签的文本。通过阅读您的描述,您似乎需要根据在特定组件中选择的行来设置标签。也许下面就是你所追求的。
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
switch (component)
case 0:
_label1.text = [othercolourArray objectAtIndex: row];
break;
case 1:
_label2.text = [otherseasonArray objectAtIndex: row];
break;
case 2:
_label3.text = [otherotherArray objectAtIndex: row];
break;
default:
break;
【讨论】:
【参考方案2】:我发现问题,只需要更换:
_label1.text = [othercolourArray objectAtIndex: row];
与
_label1.text = [othercolourArray objectAtIndex: [pickerView selectedRowInComponent:0]];
其他组件等等,谢谢!
【讨论】:
以上是关于标签上显示的 UIPickerView 选项的主要内容,如果未能解决你的问题,请参考以下文章
在 uitableviewcell 中自定义 uipickerview
具有三个组件的自定义 UIPickerView,每个组件在选择指示器上显示标签