0

我想将 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];
}

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

4

4 回答 4

0

您需要从其他视图显示您的选择器,例如 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]; 
于 2013-08-19T09:25:23.147 回答
0

你的方法应该是


- (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;
 }
}
于 2013-08-19T09:25:50.510 回答
0

试试这种方式来初始化选择器

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

这是 UIPickerview 上的一个很好的参考点

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

于 2013-08-19T09:26:42.893 回答
0

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


  - (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];
 }


于 2013-08-19T09:34:05.620 回答