1

I have a UITextField, and when pushed a UIPickerView comes up to choose a value. How do I get the UIPickerView to dismiss once a value is chosen. Someone in another thread told me to resignFirstResponder the textfield, but my code isn't working. Any ideas? NOTE: I have two text fields and UI Pickers, that's why I have the 'if' 'else' statement.

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.ageTextField)
    {
        [ageTextField resignFirstResponder];
        [agePickerView removeFromSuperview];
        return YES;
    }
    else
    {
        [relationshipTextField resignFirstResponder];
        [relationshipPickerView removeFromSuperview];
        return YES;
    }
}
Girish
  • 4,692
  • 4
  • 35
  • 55
user1681673
  • 368
  • 1
  • 6
  • 28

3 Answers3

0

Try the following code which may solve your problem.

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
 {
     if (textField == self.ageTextField) 
     {
         [ageTextField resignFirstResponder];
     } 
     else 
     {
         [relationshipTextField resignFirstResponder];
     }
     [pickerView removeFromSuperview];
     return YES; 
 }

or resign your keyboard on following method of UIPickerView.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
Girish
  • 4,692
  • 4
  • 35
  • 55
  • It didn't work. There were a couple errors with [pickerView removeFromSuperview]. – user1681673 Dec 18 '12 at 05:58
  • pickerView means the view on which you populating the UIPickerView. – Girish Dec 18 '12 at 06:02
  • Ok, I tried it and it still didn't work. Maybe I'm not testing it correctly in the simulator. What would I have to click in the simulator for the UI Picker to dismiss? – user1681673 Dec 18 '12 at 06:14
  • I tried with the same way as per your question & works fine for me may be you are missing something. I tried it when you tell me that I have couple of errors please check again & let me know. – Girish Dec 18 '12 at 06:18
  • Hey Grish, I edited my code to what I added into the program. Would that work? Also, in the simulator, what would I have to click for the UI Picker to dismiss? Thanks for the help. – user1681673 Dec 18 '12 at 21:40
0

Please try resigning the UITextField in UIPickerViewDelegate method as follows.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [self.ageTextField resignFirstResponder];
    pickerView.hidden = YES; //Show the pickerview again when you neeed it
}
Paramasivan Samuttiram
  • 3,728
  • 1
  • 24
  • 28
  • When I implement your code the UI Picker doesn't come up at all when I click the UI Textfield. Maybe I'm putting it in the wrong place though. – user1681673 Dec 18 '12 at 21:46
0

Implement the UIPickerViewDelegate protocol and implement the following method:

- (void) pickerView: (UIPickerView*) pickerView
       didSelectRow: (NSInteger)     row
        inComponent: (NSInteger)     component
{
  [self.ageTextField resignFirstResponder];
}
Kenn Cal
  • 3,659
  • 2
  • 17
  • 18