【问题标题】:UIPickerView select first row programmaticallyUIPickerView 以编程方式选择第一行
【发布时间】:2013-01-30 13:28:42
【问题描述】:

这样的问题很多,但我相信我的情况是独一无二的。

我有一个在内部单击文本框时弹出的选择器视图。用户在选择器视图中选择他们的位置,并将其放入文本框中。

打开pickerview时,滑块位于第一个元素上,但如果我单击完成按钮以最小化pickerview,则没有选择任何元素(即您必须向下滚动并返回才能选择第一个元素)

我试过了

    [pickerView selectRow:0 inComponent:0 animated:YES];

以及它的各种组合,但它只是将滑块放在该元素上,而不是实际选择它。

一些答案​​说在 viewDidLoad() 方法中应用上述代码,但不幸的是,我从 Web 服务中拉入位置数组并且不能这样做。

理想情况下,我想这样工作 - 用户在文本框中单击,pickerview 会正常弹出,如果此时单击完成按钮,则选择第一项。

提前谢谢你

【问题讨论】:

    标签: ios xcode uipickerview


    【解决方案1】:

    Swift 3+

    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == yourTextField{
            self.yourPickerView.selectRow(0, inComponent: 0, animated: true)
            self.pickerView(yourPickerView, didSelectRow: 0, inComponent: 0)
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    
    //Do all the stuff to populate the TextField
    
    }
    

    【讨论】:

    • 我们不必忘记 textField 的委托。 yourTextField.delegate = self
    【解决方案2】:

    您可以尝试以下方法:

    在您的@interface 中设置一个变量,例如NSString *selectedString;

    当您初始化UIPickerView(在pickerView:titleForRow:forComponent: 中)时,当您为第0 行设置标题时,使用与第0 行标题相同的字符串设置selectedString。然后,在pickerView:didSelectRow:inComponent: 中,将selectedString 设置为适当的字符串:

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
        // Some stuff
    
        // Assuming "yourDataArray" contains your strings
        selectedString = [yourDataArray objectAtIndex:row];
    
    }
    

    然后,当您调用由按下“完成”按钮触发的方法时,使用selectedString 设置文本。

    希望这对你有用!

    【讨论】:

      【解决方案3】:
      -(void)textFieldDidBeginEditing:(UITextField *)textField
      {
          // Only for the text field with the picker
          if (textField == self.yourTextField && [textField length]!=0)
          {
              // Select the first row programmatically 
              [self.categoriesPicker selectRow:0 inComponent:0 animated:YES];
              // The delegate method isn't called if the row is selected programmatically
              [self pickerView:self.categoriesPicker didSelectRow:0 inComponent:0];
          }
      
      
      
      }
      
      - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
      {
          //Do all the stuff to populate the TextField
      }
      

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题。我的解决方案是这样的:

        -(void)textFieldDidBeginEditing:(UITextField *)textField
        {
            // Only for the text field with the picker
            if (textField == self.yourTextField)
            {
                // Select the first row programmatically 
                [self.categoriesPicker selectRow:0 inComponent:0 animated:YES];
                // The delegate method isn't called if the row is selected programmatically
                [self pickerView:self.categoriesPicker didSelectRow:0 inComponent:0];
            }
        
        
        
        }
        
        - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
        {
            //Do all the stuff to populate the TextField
        
        }
        

        【讨论】:

        • 还需要放一个条件(如果textfield不为空)
        【解决方案5】:

        /为带有事件编辑的文本字段创建一个 IBACTION 确实开始了,该函数应该像这样检查字段是否为空/

        -(IBAction)asignFirst:(UITextField *)sender{
        if ([sender.text isEqualToString:@""])
        {
            [yourPicker selectRow:0 inComponent:0 animated:YES]; 
            sender.text = self.pickerData[0];
           }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-19
          • 1970-01-01
          • 1970-01-01
          • 2011-01-03
          • 2012-11-28
          相关资源
          最近更新 更多