Picker View选项填充标签

时间:2014-10-20 03:26:19

标签: ios objective-c uilabel uipickerview

当用户选择某些选项时,我希望我的选择器视图填充标签。 示例:组件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 个答案:

答案 0 :(得分:0)

您在didSelectRow方法中的每个break语句中都缺少case语句。

您可能还想考虑使用Modern Objective-C,https://developer.apple.com/library/ios/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html,它更容易阅读