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

Iphone UIPickerView作为按钮操作&;根据选择器的选定值设置文本字段的文本,iphone,ios,uipickerview,Iphone,Ios,Uipickerview,我想将UIPickerView显示为按钮操作&选择器所选行的文本将更新为文本字段的文本,以便用户可以使用选择器的所选值在文本字段中手动输入额外文本。所以,我这样做了: - (IBAction)commandButton:(id)sender { commandPicker = [[UIPickerView alloc]init]; commandPicker.delegate = self; commandPicker.dataSource = self; [commandPicker size

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

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

您需要从其他视图显示选择器,例如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];
}
[commandPicker selectRow:yourTextFieldRow inComponent:0 animated:YES]; 
要设置选择器的选定值,请使用
selectRow:inComponent:animated:
方法。例如:

    - (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];
}
[commandPicker selectRow:yourTextFieldRow inComponent:0 animated:YES]; 
你的方法应该是


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

尝试用这种方法初始化选择器

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


使用deelgate方法中的didSelectrow方法查找所选的行,并从数据源数组中获取值,并将其设置为textfield的text属性。

您最好为picker视图创建一个出口,连接数据源和委托,试试这个


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


因此,无论选取器是否显示,在我添加子视图后,您似乎错过了视图中的addsubview部分。应用程序显示lldb错误。将错误添加到问题并更新代码。在添加到视图之前,您的选取器是否已在视图中?请粘贴错误。错误仅为(lldb)
  - (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];
 }