2011-08-21 69 views
2

嗨,我正在开发一个iPhone应用程序,我已经填充了一个uipickerview,它可以与一个uitextfield很好地工作。但是我需要填充6个uitextfield,并且每个UItextfield都使用相同的uipickerview。UIpickerView在同一个ScrollView中填充多个uitextFields

我有工作的Ipickerview加载数组对象,它会弹出时,每个字段被触摸。问题出在UItextfields下面的代码与picker共享相同的数据。

我无法弄清楚如何编写代码,以便每个字段从UIPickerView行获取它自己的数据。 我在做什么错?任何编码建议?

感谢

@implementation BestViewController

-(NSInteger)numberOfComponentsInPickerView: (UIPickerView *)thePickerView 
{ return 1; } 

-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:     (NSInteger)component 
{return [list count]; } 

-(NSString *)pickerView:(UIPickerView *)thePickerview titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{return [list objectAtIndex:row]; } 

-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent:  (NSInteger)component 
{ 
    uitextfield1.text = [list objectAtIndex:row]; 
    utitextfield2.text = [list objectAtIndex:row]; 
} 



- (void)viewDidLoad { 

[super viewDidLoad]; 

    uitextfield1.inputView = pickerView; 
uitextfield2.inputView = pickerView; 

list = [[NSMutableArray alloc] init]; 
[list addObject:@"a"]; 
[list addObject:@"b"]; 
    [list addObject:@"c"]; 
    [list addObject:@"d"]; 
}  
+0

这是怎么回事?您的文本字段的文本是否发生了变化? – Mundi

+0

在您的代码示例中,选择器行的一个选择将同时更新两个UITextFields。这是有意的,对吧? – Mundi

+0

不,我想选择每个UItextfield并输入彼此独立的拾取器数据。我有一个文本字段的代码。但我上面编码的方式(我知道这是错误的)它使用相同的数据更新两个字段。我希望每个UITextfield具有彼此独立的数据。 –

回答

2

你需要获得当前第一响应者的持有,并设置其属性text而不是明确地设定一个特定的文本字段。

所以,

-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent:  (NSInteger)component 
{ 
    // textFields is an NSArray holding each of the textfields that are using the picker as an input view 
    for (UITextField textField in textFields) 
    { 
     if ([textField isFirstResponder]) 
     { 
      textField.text = [list objectAtIndex:row]; 
      break; 
     } 
    }  
} 

有可能是为了寻找当前第一响应者更优雅的方式,这是从我的头顶。 textFields可能是一个IBOutletCollection,但我没有使用这些我自己,所以我不能说太多的权威。

+0

谢谢,所以我将它添加到我的M文件中,我需要对我的(void)pickerView代码做出什么更改? - (BOOL)acceptFirstResponder { return YES; } –

+0

不,选择器不是第一响应者,您的文本框是。活动文本字段将向isFirstResponder返回YES。 – jrturton

+0

对不起,让我重新输入我的回复。在我的M文件中添加 - (布尔)acceptedFirstResponder {返回YES; }我的问题是我如何将我的 - (void)pickerView指向活动的UITextfield?现在我命名一个特定的字段uitextfield1.text = [list objectAtIndex:row]我希望它是通用的并加载活动字段。 –

2

下面是我该怎么做:

首先,定义几个不同的选择器。当然,您也可以使用相同的UIPickerView,但您更改了一个可帮助您区分它们的属性。每个文本字段(几乎)都有不同的数据。 Apple为此设计的一个便利特性是tag,这是一个可用于每个UIView的任意整数。您可以将相同的标签分配给UITextField

例如:

#define kFirstTextField 101 
#define kSecondTextField 102 
#define kThirdTextField 103 
//... etc 

在该方法中,当文本字段被感动:

[myPickerView setHidden:NO]; // or however you show the picker 
[myPickerView setTag:textField.tag]; 
[myPickerView reloadAllComponents]; 

在选择器的数据的方法:

-(NSString *)pickerView:(UIPickerView *)thePickerview 
      titleForRow:(NSInteger)row 
      forComponent:(NSInteger)component 
{ 
    if (thePickerView.tag==kFirstTextField) 
     { return [list1 count]; } 
    if (thePickerView.tag==kSecondTextField) 
     { return [anotherOrTheSameList count]; } 
// of course, you can also use a switch statement 
    return [defaultList count]; 
} 

做类似的东西方法titleForRow:
最后,事情是采摘时,按标签再次歧视:

-(void) pickerView:(UIPickerView *)thePickerview 
     didSelectRow:(NSInteger)row 
     inComponent:(NSInteger)component 
{ 
    UITextField *field = (UITextField *) [thePickerview.superview viewWithTag:thePickerview.tag]; 
// this assumes the UIPickerView is the subview of the same view as the UITextFields 
    if (thePickerview.tag==kFirstTextField) 
     { field.text = [list1 objectAtIndex:row]; } 
    if (thePickerview.tag==kSecondTextField) 
     { field.text = [anotherOrTheSameList objectAtIndex:row]; } 
    // etc. 
    // alternatively: 
    field.text = [self pickerView:thePickerview titleForRow:row forComponent:0]; 
} 
+0

我的天啊......你到底在哪里得到这些想法......它的辉煌。谢谢@Mundi :) –