如何将两个NSAarray传递给基于UITextFields的UIPickerView

时间:2014-07-20 10:40:40

标签: ios objective-c uitextfield nsarray uipickerview

我在textField时单击,打开UIPickerView然后选择它在UITextField上显示的PickerView数据。 但现在我有3个text_Filed(城市,打字机,分支机构),但我不知道单UIPickerView上的三个TextFileds。所以请给我任何想法。

单个文本 - 提交的一个UIPickerview代码是: -

Businessname=[[UITextField alloc]init];
    Businessname.frame=CGRectMake(0, 0, 221, 33);
    Businessname.placeholder=@"Business Name";
    [Businessname setFont:[UIFont fontWithName:@"Times New Roman" size:13]];
    Businessname.backgroundColor=[UIColor whiteColor];
    [Businessname setBorderStyle:UITextBorderStyleBezel];
   Businessname.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
   Businessname.layer.borderColor=[[UIColor yellowColor]CGColor];
   Businessname.layer.borderWidth= 2.0f;
    Businessname.textColor=[UIColor blackColor];
    [scrol addSubview:Businessname];
   yourpicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 400, 100, 150)];
    [yourpicker setDataSource: self];
    [yourpicker setDelegate: self];
    yourpicker.showsSelectionIndicator = YES;
    Businessname.inputView = yourpicker;
    [yourpicker  setShowsSelectionIndicator:YES];

mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 300, 100, 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];
    Businessname.inputAccessoryView = mypickerToolbar;

-(void)pickerDoneClicked

{
    NSLog(@"Done Clicked");
     [Businessname resignFirstResponder];
    mypickerToolbar.hidden=YES;
    yourpicker.hidden=YES;
}
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    return [pickarray count];
}

输出上面的代码是当我单击文本字段然后打开UIPIckerView然后选择数据。但是现在我有三个TextFields和3个NSArray请求如何根据UIPickerView将数据数据传递给UITextField。 所以请告诉我有关我的问题的任何想法

感谢高级

1 个答案:

答案 0 :(得分:0)

您可以通过为每个文本字段设置标记值来完成此操作。还为每个文本字段设置UITextField的委托。 在 UITextField

的委托方法中
- (void)textFieldDidBeginEditing:(UITextField *)textField

通过它的标记值来识别文本字段。 基于标签值执行类似

的操作

//假设100,101,102是textfield&#39的标签值

NSInteger tag = textField.tag;

switch(tag)
{
  case 100:
     pickarray = @[@"1",@"2",@"3",@"4"];
     break;
  case 101:
     pickarray = @[@"10",@"20",@"30",@"40"];
     break;
  case 102:
     pickarray = @[@"11",@"21",@"31",@"41"];
     break;
  default: 
     break;

}

通过这个你可以为" pickarray"分配不同的值。根据所选的文本字段并重新加载UIPickerView。