简体   繁体   中英

UIPickerView as a button action & set the text of a text field from the selected value of the picker

I want to show a UIPickerView as a button action & the text of the selected row of the picker will be updated as the text of a text field, so that user can manually input extra text in the text field with the selected value of the picker. so, i did this:

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

But its not working. And how can I set the text of a text field as the selected value of the picker?

You need to show your picker from other view, UIActionSheet for example:

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

For set selected value of picker use selectRow:inComponent:animated: method. For example:

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

Your method should be


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

In you picker delegate


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

Try this way to initialize the 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

Make use of didSelectrow method in deelgate method to find which row is selected and get the value from datasource array and set it to the text property of the textfield.

You better made an outlet for picker view, wire up datasource and delegate, try this


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


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM