UIPickerView选项显示在标签上

时间:2014-10-13 03:11:36

标签: ios objective-c iphone xcode

我的UIPickerView有3个组件,下面有三个标签。当为选择器的1个组件选择答案时,我希望第一个标签显示组件的答案。然后在第2和第3标签上显示第二和第三组分。但是,它目前没有像我想的那样工作,有什么建议吗?

我认为我的问题出在&#34 ;-( 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

2 个答案:

答案 0 :(得分:0)

我认为你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;
    }
}

答案 1 :(得分:0)

我发现了问题,只需更换:

_label1.text = [othercolourArray objectAtIndex: row];

_label1.text = [othercolourArray objectAtIndex: [pickerView selectedRowInComponent:0]];

以及其他组件等等,非常感谢!