【问题标题】:UIPickerView as a button action & set the text of a text field from the selected value of the pickerUIPickerView 作为按钮操作并根据选择器的选定值设置文本字段的文本
【发布时间】:2013-08-19 08:46:05
【问题描述】:

我想将 UIPickerView 显示为按钮操作 & 选择器所选行的文本将更新为文本字段的文本,以便用户可以在文本字段中手动输入具有所选值的额外文本的选择器。所以,我这样做了:

- (IBAction)commandButton:(id)sender {
commandPicker = [[UIPickerView alloc]init];
commandPicker.delegate = self;
commandPicker.dataSource = self;
[commandPicker sizeToFit];
commandPicker.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
commandPicker.showsSelectionIndicator = YES;
[self.view addSubview:commandPicker];
}

但它不起作用。以及如何将文本字段的文本设置为选择器的选定值?

【问题讨论】:

  • 所以选择器是否显示,似乎你错过了视图的 addsubview 部分
  • 添加子视图后,应用显示 lldb 错误。
  • 将错误添加到问题并更新代码。您的选择器在添加到视图之前是否已经在视图中?
  • 请粘贴错误。
  • 错误只是(lldb)

标签: iphone ios uipickerview


【解决方案1】:

您需要从其他视图显示您的选择器,例如 UIActionSheet:

    - (IBAction)commandButton:(id)sender {

UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"sasdasd" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 470.0)];  //your values

commandPicker = [[UIPickerView alloc]init];
commandPicker.delegate = self;
commandPicker.dataSource = self;
[commandPicker sizeToFit];
commandPicker.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
commandPicker.showsSelectionIndicator = YES;

[actionSheet addSubview:commandPicker];
}

要设置选择器的选定值,请使用selectRow:inComponent:animated: 方法。例如:

[commandPicker selectRow:yourTextFieldRow inComponent:0 animated:YES]; 

【讨论】:

    【解决方案2】:

    试试这种方式初始化picker

    UIPickerView *commandPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
    commandPicker.delegate = self;
    commandPicker.dataSource = self;  
    commandPicker.showsSelectionIndicator = YES;
    [self.view addSubview:commandPicker];
    

    Here is a good reference point on UIPickerview

    利用deelgate方法中的didSelectrow方法来查找选择了哪一行,并从数据源数组中获取值并将其设置为文本字段的文本属性。

    【讨论】:

      【解决方案3】:

      你的方法应该是

      
      - (IBAction)commandButton:(id)sender {
        commandPicker = [[UIPickerView alloc]init];
        commandPicker.delegate = self;
        commandPicker.dataSource = self;
        [commandPicker sizeToFit];
        commandPicker.autoresizingMask = (UIViewAutoresizingFlexibleWidth |     UIViewAutoresizingFlexibleHeight);
        commandPicker.showsSelectionIndicator = YES;
        [self.view addSubView:commandPicker];
      }
      

      在你的选择器委托中

      
      - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
      {
       if(pickerView == commandPicker)
       {
         NSString *myText = [yourarray objectAtIndex:row];
         self.myTextField.text = myText;
       }
      }
      

      【讨论】:

        【解决方案4】:

        你最好为选择器视图、连接数据源和委托创建一个出口,试试这个


          - (void)viewDidLoad
         {
             [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
             self.dataArray = [NSMutableArray arrayWithObjects:@"hello",@"happy",@"coding", nil];// set up your array
        //initially pickerview is hidden
             self.aPickerView.hidden = YES; //initially picker view must be hidden
        
        }
        
         - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
        {
            return [self.dataArray objectAtIndex:row];
        }
        - (IBAction)whenButtonTapped:(id)sender 
        {
             if(self.aPickerView.hidden) //it is appearing
              {
               self.aPickerView.hidden = NO;
              }
              else
              {
                 self.aPickerView.hidden = YES;
              }  
        
          }
        
           -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
         {
           //in this method set your text field's text
              self.aTextField.text = [self.dataArray objectAtIndex:row];
         }
        


        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-01-09
          • 2019-02-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-16
          • 2014-01-03
          相关资源
          最近更新 更多