【问题标题】:How to use one UIPickerView for multiple textfields in one view?如何在一个视图中将一个 UIPickerView 用于多个文本字段?
【发布时间】:2012-07-23 12:23:06
【问题描述】:

我在一个视图中有 8 个文本字段。我想使用pickerview填充每个。 我可以有 1 个pickerview 来为不同的文本字段显示来自不同数组的不同项目列表吗?

谁能解释我如何使用示例代码以编程方式进行操作?

此外,如何在视图加载时隐藏选择器视图,并且仅当我单击带有相应数据列表的文本字段时才应显示它?当我选择数据时它应该会消失,当我单击具有相应数据列表的不同文本字段时它会重新出现,依此类推。

我是 xcode 的新手。任何帮助都感激不尽。谢谢。

【问题讨论】:

    标签: iphone xcode uitextfield uipickerview


    【解决方案1】:

    您只能使用一个 PickerView,根据某些条件显示不同的数据。

    我的想法如下:您可以创建,比如说,8 个不同的数组 - 一个用于填充 UIPickerView,具体取决于所点击的 UITextField。在接口中声明为 NSArray 并在 viewDidLoad 中初始化:

    array1st = ...
    array2nd = ...
    array3d = ...
    //and until 8.
    

    然后你创建另一个只是为了指向应该填充它的数组(如NSArray *currentArray),你甚至不需要初始化它=它只会用于指向正确的数组。

    因此,您应该将 UITextFields 的委托设置为您的视图控制器,并在方法 textFieldShouldBeginEditing 中检查谁是 UITextField,将正确的数组分配为 currentArray,使用 reloadData 重新加载 UIPickerView,然后返回 NO在相同的textFieldShouldBeginEditing 方法中。 currentTextField 是一个指针,仅用于知道正在“编辑”的 currentUITextField 是什么 - 我们需要存储它以便能够在选择器中选择数据后设置文本。在界面中声明currentTextField。

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        currentTextField = textField;
        if (textField == myFirstTextView)
        {
            currentArray = array1st;
            [pickerView reloadData];
            [self animatePickerViewIn];
            return NO;
        }
        //and this way until 8th
    }
    

    所以,当你的视图控制器,实际上是 UIPickerViewDelegate 时,你根据当前数组填充你的 UIPickerView 并且你不需要改变任何东西,只需使用 currentArray 作为数组来获取数据。

    现在,显示pickerView:

    连接 IBOutlet 后,在 viewDidLoad 中你应该做两件事:设置 UIPickerView 的框架并将其添加为子视图:

    pickerView.frame = CGRectMake(0,self.view.frame.size.height, pickerView.frame.size.width, pickerView.frame.size.height);
    pickerView.delegate = self;
    pickerView.dataSource = self;
    //or you can do it via Interface Builder, right click the pickerView and then connect delegate and dataSource to the file's owner
    [self.view addSubview:pickerView];
    

    现在您需要创建名为 animatePickerViewIn 的方法,当用户点击 UITextField 时我们会调用该方法。

    -(void)animatePickerViewIn
    {
    
        [UIView animateWithDuration:0.25 animations:^{
            [pickerView setFrame:CGRectMake(0, pickerView.frame.origin.y-pickerView.frame.size.height, pickerView.frame.size.width, pickerView.frame.size.height)];
        }];
    }
    

    在pickerView中加载数据:

    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
    
        return [currentArray count];
    }
    
    
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 1;
    }
    
    -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return [currentArray objectAtIndex:row];
    }
    

    当用户在pickerView 中选择行时,您将收到它作为委托方法。所以只需将 currentTextField 的文本设置为选定的值即可:

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        //and here you can do in two ways:
        //1
        [currentTextField setText:[currentArray objectAtIndex:row]];
        //2
        [currentTextField setText:[self pickerView:pickerView titleForRow:row inComponent:component]];
    }
    

    【讨论】:

      【解决方案2】:

      使用这个。

       - (void)textFieldDidBeginEditing:(UITextField *)textField{
         MyPickervie.delegate=self;
       }
       - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)myLocationPickerView;
      {
        return 1;
       }
      
      -(void)myLocationPickerView:(UIPickerView *)myLocationPickerView didSelectRow:           (NSInteger)row inComponent:(NSInteger)component;
       {
      
          switch(textfield.tag){
           Case 0:
                  label.text=[arraNo objectAtindex:row];
                  break;
           default: break;
        }
       }
       -(NSInteger)myLocationPickerView:(UIPickerView *)myLocationPickerView  numberOfRowsInComponent:(NSInteger)component;
      {
      
      switch(textfield.tag){
           Case 0:
                  return [arrayNo count];
                  break;
           default: break;
        }
      }
      
      -(NSString *)myLocationPickerView:(UIPickerView *)myLocationPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
       {
         switch(textfield.tag){
           Case 0:
                  return [arrayNo objectAtIndex:row];
                  break;
           default: break;
        }
      
      }
      

      你必须在 switch 条件下使用 8 个 Case 来调用不同的数组并将其设置到 pickerView 中。

      【讨论】:

      • 嗨,你能解释一下什么是 textfield.tag 吗?它说在切换案例中未声明文本字段?和 myLocationPickerView 我们将文本设置为哪个 label.text?
      • textfield.tag 您可以在其中使用您在 .h 文件中声明的文本字段和 IBOutlate 它。并且从选择器视图中选择的值使用 label.text 显示在标签中。你可以使用你的标签或任何你想显示所选值的地方。
      【解决方案3】:

      我已经用 3 个文本字段测试了这段代码。

      并找到了您问题的完美解决方案。

      1) 取一个全局 UITextField。

      2) 在文本字段编辑的委托方法中,将全局文本字段设置为当前文本字段。

      3) 在选择器值更改方法上,设置全局文本字段的文本,它将更改选定文本字段的值。

      以下代码运行正常。

      享受编码。

      - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView 
      {
      return 1;
      }
      
      - (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component 
      {   
      return 10;
      }
      
      - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
      {
      return [NSString stringWithFormat:@"Test %d",row + 1];
      
      }
      
      - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
      {
      
      test.text = @"XYZ";
      
      }
      
      - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
      {
      test = textField;
      
      return YES;
      }
      
      - (BOOL)textFieldShouldReturn:(UITextField *)textField
      {    
      [textField  resignFirstResponder];
      
      return YES;
      }
      

      【讨论】:

      • 您好,抱歉,由于我对 xcode 完全陌生,因此无法按照您的指示进行操作。您能否详细说明或解释您的第 2 点和第 3 点?
      • - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 和 - (BOOL)textFieldShouldReturn:(UITextField *)textField 是 UITextField 的委托方法。当显示键盘时,它将管理键盘功能。在这种情况下,您只需隐藏键盘并显示选择器。
      • 对于选择器的前 4 个方法是委托方法,它将管理选择器的所有基本功能。即选择器中的行数,选择器中的数据,选择器值发生变化时该怎么办等等......您需要在视图控制器的xib和.h文件中设置委托。
      • 谢谢禾芒。我已经设法部分使用 Natan R 的解决方案解决了这个问题。我肯定会在我的下一个应用程序中尝试你的方法。非常感谢!感谢您的帮助。
      【解决方案4】:

      1.使用带有Done barButtonItem的工具栏,它应该隐藏pickerView。

      2.现在给你的 textFields tagValues 像:textFiled1.tag=100; 然后使用这个标签值填充你的 pickerView 数组。

      3.当用户从pickerView中选择任意Value时,在其委托方法中写入

      if(textField.tag== @"Your Tag Value")
      {  
            textField.text=[pickerArray objectAtIndex:row];
      }
      

      我想你可以自己尝试代码,现在你已经掌握了解决方案的要点。祝你好运!!

      【讨论】:

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