选择器视图选项填充标签
Posted
技术标签:
【中文标题】选择器视图选项填充标签【英文标题】:Picker View choices populate labels 【发布时间】:2014-10-20 03:26:19 【问题描述】:我希望我的选择器视图在用户选择某些选项时填充标签。 示例:组件 1 选项将显示在标签 1 中,组件 2 选项将显示在标签 2 中,组件 3 选项将显示在标签 3 中。
我对普通的选择器没有问题,但是我的选择器组件会根据前一个组件的选择而变化。
例子:
组件1中的第一选择显示组件2中的数据
组件 1 中的第二个选择显示组件 2 中的不同数据
依次在组件 3 中显示不同的数据
它目前没有按我的意愿运行和运行,我不知道我做错了什么。我认为这是我的“didSelectRow”部分,但不完全确定。
.h
#import <UIKit/UIKit.h>
@interface ViewController : 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
.m
#import "ViewController.h"
@interface ViewController ()
NSArray*overalltype;
NSArray*healthtype;
NSArray*midwifetype;
NSArray*doctortype;
NSArray*otherhealthtype;
NSArray*visitorstype;
NSArray*othertype;
NSArray*onlinetype;
NSArray*volunteertype;
NSInteger selectedOptionFromColumn1;
NSInteger selectedOptionFromColumn2;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
overalltype =[[NSArray alloc]initWithObjects:@"Health Professional", @"HouseKeeping",@"Visitors", @"Other",nil];
healthtype =[[NSArray alloc]initWithObjects:@"Midwife", @"Nurse",@"Doctor", @"Other Health Professional" ,nil];
midwifetype =[[NSArray alloc]initWithObjects:@"Known Midwife", @"New Midwife", nil];
doctortype =[[NSArray alloc]initWithObjects:@"Snr Dr", @"Jnr Dr", @"Baby Dr", @"GP", nil];
otherhealthtype =[[NSArray alloc]initWithObjects:@"Lactation Consultant", @"Student", @"Hearing Technician", @"Bloody Collector", @"Social Worker", @"Mental Health", @"Physiotherapist", nil];
visitorstype =[[NSArray alloc]initWithObjects:@"Partner", @"Relatives", @"Friends", @"Other Woman's Visitors", nil];
othertype =[[NSArray alloc]initWithObjects:@"Online Support", @"Volunteer", @"Church Group", @"Wardsperson", nil];
onlinetype =[[NSArray alloc]initWithObjects:@"Chatroom", @"Internet Search", nil];
volunteertype =[[NSArray alloc]initWithObjects:@"ABA", @"Hospital Volunteer", @"Church Group", nil];
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
return 3;
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
switch (component)
case 0:
return overalltype.count;
break;
case 1:
if (selectedOptionFromColumn1 == 0)
return healthtype.count;
else if (selectedOptionFromColumn1 == 2)
return visitorstype.count;
else if (selectedOptionFromColumn1 == 3)
return othertype.count;
break;
case 2:
if (selectedOptionFromColumn1 == 0 && selectedOptionFromColumn2 ==0)
return midwifetype.count;
else if (selectedOptionFromColumn1 == 0 && selectedOptionFromColumn2 ==2)
return doctortype.count;
else if (selectedOptionFromColumn1 == 0 && selectedOptionFromColumn2 ==3)
return otherhealthtype.count;
else if (selectedOptionFromColumn1 == 3 && selectedOptionFromColumn2 ==0)
return onlinetype.count;
else if (selectedOptionFromColumn1 == 3 && selectedOptionFromColumn2 ==1)
return volunteertype.count;
break;
default:
break;
return 0;
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
switch (component)
case 0:
return [overalltype objectAtIndex:row];
break;
case 1:
if (selectedOptionFromColumn1 == 0)
return [healthtype objectAtIndex:row];
else if (selectedOptionFromColumn1 == 2)
return [visitorstype objectAtIndex:row];
else if (selectedOptionFromColumn1 ==3)
return [othertype objectAtIndex:row];
break;
case 2:
if (selectedOptionFromColumn1 == 0 && selectedOptionFromColumn2 == 0)
return [midwifetype objectAtIndex:row];
else if (selectedOptionFromColumn1 == 0 && selectedOptionFromColumn2 == 2)
return [doctortype objectAtIndex:row];
else if (selectedOptionFromColumn1 == 0 && selectedOptionFromColumn2 == 3)
return [otherhealthtype objectAtIndex:row];
else if (selectedOptionFromColumn1 == 3 && selectedOptionFromColumn2 == 0)
return [onlinetype objectAtIndex:row];
else if (selectedOptionFromColumn1 == 3 && selectedOptionFromColumn2 == 1)
return [volunteertype objectAtIndex:row];
break;
default:
break;
return 0;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
switch (component)
case 0:
selectedOptionFromColumn1=[pickerView selectedRowInComponent:0];
[pickerView reloadComponent:1];
_label1.text = [overalltype objectAtIndex: [pickerView selectedRowInComponent:0]];
case 2:
selectedOptionFromColumn2=[pickerView selectedRowInComponent:1];
[pickerView reloadComponent:2];
if(selectedOptionFromColumn1 == 0)
_label2.text = [healthtype objectAtIndex: [pickerView selectedRowInComponent:1]];
else if(selectedOptionFromColumn1 ==2)
_label2.text = [visitorstype objectAtIndex: [pickerView selectedRowInComponent:1]];
else if(selectedOptionFromColumn1 ==3)
_label2.text = [otherhealthtype objectAtIndex: [pickerView selectedRowInComponent:1]];
case 3:
if (selectedOptionFromColumn1 ==0 && selectedOptionFromColumn2 == 0)
_label3.text = [midwifetype objectAtIndex: [pickerView selectedRowInComponent:2]];
else if(selectedOptionFromColumn1 ==0 && selectedOptionFromColumn2 ==2)
_label3.text = [doctortype objectAtIndex: [pickerView selectedRowInComponent:2]];
else if(selectedOptionFromColumn1 ==0 && selectedOptionFromColumn2 == 3)
_label3.text = [otherhealthtype objectAtIndex: [pickerView selectedRowInComponent:2]];
else if(selectedOptionFromColumn1 ==3 && selectedOptionFromColumn2 == 0)
_label3.text = [onlinetype objectAtIndex: [pickerView selectedRowInComponent:2]];
else if(selectedOptionFromColumn1 ==3 && selectedOptionFromColumn2 == 1)
_label3.text = [volunteertype objectAtIndex: [pickerView selectedRowInComponent:2]];
- (void)didReceiveMemoryWarning [super didReceiveMemoryWarning];
@end
【问题讨论】:
什么错误?您需要提供更多详细信息以供任何人提供帮助。 另外,为了将来,尝试将您的代码格式化为尽可能可读。这次给你做了,你看一下,对我们理解你的问题很有帮助。 【参考方案1】:您在 didSelectRow 方法中缺少 break
语句作为每个 case
语句的一部分。
您可能还想考虑使用 Modern Objective-C,https://developer.apple.com/library/ios/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html,它更容易阅读
【讨论】:
以上是关于选择器视图选项填充标签的主要内容,如果未能解决你的问题,请参考以下文章