UIPickerView 作为按钮操作并根据选择器的选定值设置文本字段的文本

Posted

技术标签:

【中文标题】UIPickerView 作为按钮操作并根据选择器的选定值设置文本字段的文本【英文标题】:UIPickerView as a button action & set the text of a text field from the selected value of the picker 【发布时间】: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) 【参考方案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];
 

【讨论】:

以上是关于UIPickerView 作为按钮操作并根据选择器的选定值设置文本字段的文本的主要内容,如果未能解决你的问题,请参考以下文章

如果我在选择器旋转时使用完成按钮关闭 UIPickerview 的操作表,则获取空值

如何根据按钮按下重新加载/刷新 UIPickerView(带有新数据数组)?

无法根据选择从 UIPickerView 中删除项目

UIPickerview 完成按钮不起作用,即使它没有调用选择器方法

延迟一段时间后显示 uipickerview

横向操作表中的 UIPickerView