【问题标题】:how to pass two NSAarray to One UIPickerView Based on UITextFields如何将两个 NSAarray 传递给一个基于 UITextFields 的 UIPickerView
【发布时间】:2014-07-20 10:40:39
【问题描述】:

当我点击textField 时,打开UIPickerView 然后选择它在UITextField 上显示的PickerView 数据。 但现在我有 3 个 text_Filed(city,typeofcars,branches) 但我不知道 Single UIPickerView 上的三个 TextFiled。所以请给我任何想法。

单个文本-归档一个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然后选择数据时输出的。但是现在我有 3 个 TextFields 和 3 个 NSArrays 请如何根据 UITextField 将数据数据传递给 UIPickerView。 所以请给我关于我的问题的任何想法

感谢进阶

【问题讨论】:

  • 也许如果你简化你的代码示例,去掉与这个问题无关的代码,我们就能更快地回答你
  • 为每个UITextField添加标签怎么样?然后你之前检查一下,根据字段标签加载数据。

标签: ios objective-c uitextfield nsarray uipickerview


【解决方案1】:

您可以通过为每个文本字段设置标签值来做到这一点。还为每个文本字段设置 UITextField 的委托。 在 UITextField

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

通过它的标签值识别文本字段。 基于标签值做类似的事情

// 假设 taht 100, 101, 102 是 textfield 的标签值

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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多