无法在选择器视图中选择单值数组

时间:2016-07-12 06:49:00

标签: ios objective-c

拥有单个值数组,我想将该单个值选为textfiled文本。加载pickerview后,我无法选择该单个值,因为在选择该单行值时,视图是滚动而选择器视图正在滚动。

我无法选择单行值,我尝试这样:

Picker view decleration

 ProjectListpicker = [[UIPickerView alloc] initWithFrame:CGRectMake(325, 90,   self.view.frame.size.width-500, 300)];
[ProjectListpicker setShowsSelectionIndicator:YES];
[ProjectListpicker selectRow:1 inComponent:0 animated:YES];
[ProjectListpicker reloadAllComponents];
ProjectListpicker.delegate = self;
ProjectListpicker.dataSource = self;
ProjectListpicker.tag = 1;
ProjectListpicker.backgroundColor=[UIColor yellowColor];
[ProjectListpicker selectRow:1 inComponent:0 animated:YES];
ProjectListpicker.alpha = 0;
[CreatmeetingView addSubview:ProjectListpicker];

didSelectRow方法

    Projectstxtfld.text=[PROJECT_NAMEArray objectAtIndex:row];
    _ProjId = [PROJECT_IDArray objectAtIndex:row];
    NSLog(@"propiid %@ %@",ProjectIdstr,_ProjId);
   [ProjectListpicker removeFromSuperview];

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

试试以下代码

UIPickerView * ProjectListpicker = [[UIPickerView alloc] init];
    ProjectListpicker.frame = CGRectMake(5,70,150,300);
    ProjectListpicker.tag = 1;
    ProjectListpicker.delegate = self;
    ProjectListpicker.showsSelectionIndicator = YES;
    [self.view addSubview:ProjectListpicker];

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"%@",_pickerData[row]);
}

答案 1 :(得分:0)

我为你的问题尝试了示例项目。我们必须使用PickerView的委托方法。在arrayPicker中我只有一个数据。那就是iOS。当我选择组件中的行时,它完美地工作,我得到所选的行textField中的值。

数组中只有一个数据,因此如果要直接设置所选行并获取该值,请使用以下代码。此处选择行必须为0.

[ProjectListpicker selectRow:0 inComponent:0 animated:YES];

而不是[ProjectListpicker selectRow:1 inComponent:0 animated:YES];

UIPickerView * ProjectListpicker = [[UIPickerView alloc]init];
ProjectListpicker.frame = CGRectMake(0, 315, 320, 216);
ProjectListpicker.showsSelectionIndicator = YES;
ProjectListpicker.dataSource = self;
ProjectListpicker.delegate = self;
[ProjectListpicker selectRow:0 inComponent:0 animated:YES];
[self.view addSubview:ProjectListpicker];

pragma mark -PickerView Delegate

// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
  return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
   return arrayPicker.count;
}

// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [arrayPicker objectAtIndex:row];
}

// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
   return 300;
}

// get Picker Value
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   @try {
     textFldPickerValue.text = [arrayPicker objectAtIndex:row];
     NSLog(@"The textFldPickerValue.text is - %@",textFldPickerValue.text);
   }
   @catch (NSException *exception) {

   }
 }