在选择器视图中显示上一个数组

时间:2016-05-06 09:46:35

标签: ios uitextfield uipickerview

我是IOS的新手我希望在点击文本字段时在选择器视图中显示特定数组。我创建了两个文本字段和两个带有一个选择器视图的数组。 我运行我的项目成功但失败当我选择第一个文本字段第二个数组显示在选择器视图和第二个文本字段在选择器视图中单击其第二个数组我不想在第一个文本字段中的第二个数组。

Picker视图编码:

pktStatePicker = [[UIPickerView alloc] initWithFrame:CGRectZero];

    pktStatePicker  .delegate = self;

    pktStatePicker  .dataSource = self;

    [ pktStatePicker  setShowsSelectionIndicator:YES];

   txtText.inputView =  pktStatePicker  ;
    currency1.inputView = pktStatePicker;

    // Create done button in UIPickerView


    UIToolbar*  mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];

    mypickerToolbar.barStyle = UIBarStyleBlackOpaque;

    [mypickerToolbar sizeToFit];


    NSMutableArray *barItems = [[NSMutableArray alloc] init];


    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

    [barItems addObject:flexSpace];


    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked)];

    [barItems addObject:doneBtn];


    [mypickerToolbar setItems:barItems animated:YES];


    txtText.inputAccessoryView = mypickerToolbar;
    currency1.inputAccessoryView = mypickerToolbar;

}

Picker视图委托和文本字段:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    if (textField == txtText )
    {
        pktStatePicker = arrMsg;
        [pktStatePicker reloadAllComponents];

    }
    else if (textField == currency1 )
    {
        pktStatePicker = currencyname1;
        [pktStatePicker reloadAllComponents];
    }

}
-(void)pickerDoneClicked

{
    NSLog(@"Done Clicked");


    [txtText resignFirstResponder];
    //[currency1 res];

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 1;

}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{
    if(pickerView.tag == 1){
        return arrMsg.count;
    }else{
    return currencyname1.count;
    }
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{
    if(pickerView.tag==1){

        return [arrMsg objectAtIndex:row];
    }
    else{
            return [currencyname1 objectAtIndex:row];
    }

}


- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{
    if(pickerView.tag ==1){
    txtText.text = (NSString *)[arrMsg objectAtIndex:row];
    }else{
    currency1.text = (NSString *)[currencyname1 objectAtIndex:row];
    }

}

2 个答案:

答案 0 :(得分:0)

你在哪里设置pktStatePicker.tag?默认情况下,它设置为零,每次都会看到第二个数组。添加到您的代码

if(remainder == 0)

并删除

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == txtText )
{
    pktStatePicker.tag = 1;


}
else if (textField == currency1 )
{
    pktStatePicker.tag = 0;

}
  [pktStatePicker reloadAllComponents];
return YES;
}

}

这是错的。我想它可以帮助你。我没有检查

答案 1 :(得分:0)

像这样:

声明一个简单的枚举来管理.m文件顶部的选择器状态:

typedef NS_ENUM(NSUIInteger, PickerState) {
   PickerStateFirst = 0, 
   PickerStateSecond
}

声明一个PickerState变量:

@property (nonatomic) PickerState pickerState;

当textfield开始编辑时,设置选择器状态(在IB中设置):

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
  self.pickerState = textField.tag;
  [pktStatePicker reloadAllComponents];
   return YES;
} 

检查选择代表中现在的状态:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if(self.pickerState == PickerStateFirst){
        return arrMsg.count;
    }
    return currencyname1.count;
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
     if(self.pickerState == PickerStateFirst){
         return [arrMsg objectAtIndex:row];
     } 
     return [currencyname1 objectAtIndex:row]; 
}


- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if(self.pickerState == PickerStateFirst){
    txtText.text = (NSString *)[arrMsg objectAtIndex:row];
    }
    currency1.text = (NSString *)[currencyname1 objectAtIndex:row];
}