【问题标题】:UIPickerView choices shown on label标签上显示的 UIPickerView 选项
【发布时间】: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? 请详细说明或给我们错误日志之类的:(

标签: ios objective-c iphone xcode


【解决方案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]];
    

    其他组件等等,谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-22
      • 2023-03-18
      • 2019-06-23
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多