2011-04-12 76 views
1

我可以使用一个UIPickerView与许多UITextFields,或者我应该创建多个pickerviews,每个UITextField(我有同一视图中的所有UITextFields)?一个UIPickerView与多个文本域

+0

如果你有你的答案,然后点击右键给出与答案 – dks1725 2011-04-12 11:36:13

+0

好吧,但我不知道如何做到这一点。 – izan 2011-04-12 11:39:20

+0

如何接受答案? – izan 2011-04-12 11:39:45

回答

0

我觉得有多个文本框的Picker视图就足够了。您可以根据自己的需要轻松标记文本字段并对其进行处理。

Multiple sources for UIPickerView on textfield editing

http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/12952-tutorial-uipickerview-basics-basic-tip-calculator.html

将帮助您开始

+0

感谢您的回答。我该如何接受你的答案? – izan 2011-04-12 11:42:20

+0

只需点击答案左侧的勾号即可将其变为绿色 – visakh7 2011-04-12 11:43:38

2

请声明此全球textFieldName:

NSString * textFieldName; 

分配在didLoad:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [pickerView setHidden:YES]; 

    textFieldName=[NSString alloc]init]; 

    pickerArray1 = [[NSMutableArray alloc] initWithObjects:@"apple", @"mango", @"banana", nil]; 

    pickerArray2 = [[NSMutableArray alloc] initWithObjects:@"black", @"white", @"green", nil]; 
} 

请textFieldName字符串设置文本字段名称:

-(void)textFieldDidBeginEditing:(UITextField *)textField{ 
    [pickerView setHidden:YES]; 
    if (textField1.editing == YES) 
    { 
     textFieldName=textField1; 
     [pickerView setHidden:NO]; 
    } 
    else 
     if (textField2.editing == YES) 
     { 
      textFieldName=textField2; 
      [pickerView setHidden:NO]; 
     } 
    } 

然后用这个textFieldName在pickerview方法来检查:

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; 
{ 
    if (textFieldName isEqualToString:@"textField1") 
    { 
     return [pickerArray1 count]; 
    } 
    else 
     if (textFieldName isEqualToString:@"textField2") 
     { 
      return [pickerArray2 count]; 
     } 
    } 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 
{ 
    if (textFieldName isEqualToString:@"textField1") 
    { 
     return [pickerArray1 objectAtIndex:row]; 
    } 
    else 
     if (textFieldName isEqualToString:@"textField2") 
     { 
      return [pickerArray2 objectAtIndex:row]; 
     } 
    } 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    if (textFieldName isEqualToString:@"textField1") 
    { 
     textField1.text= [pickerArray1 addObjectAtIndex:row]; 
    } 
    else 
     if (textFieldName isEqualToString:@"textField2") 
     { 
      textField2.text= [pickerArray2 addObjectAtIndex:row]; 
     } 
     [pickerView setHidden:YES]; 
    } 
4
-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    if (textField1.editing == YES) 
    { 
     textFieldName=textField1; 
    } 
    else 
     if (textField2.editing == YES) 
     { 
      textFieldName=textField2; 
     } 
} 
3

使用数组列表视图,然后得到这个职位。