Search code examples
iphoneiphone-softkeyboard

hide keyboard on pickerview textfield


I have implemented a view, in which there is two text fields. At first text field, when user click on it, there is a display of picker view. When user clicks on other text field keyboard is shown. But when user go from second text field to first text field it still shows the keyboard and there is a picker view on the back of this keyboard. I am unable to resign this keyboard when user move from second to first textfield without pressing keyboard done button.

 -(void)textFieldDidBeginEditing:(UITextField *)myTextField
  {  
  if(myTextField == firsttextfield)
  {
    [firsttextfield resignFirstResponder];
    medicationtypepicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,190,320,215)];
    medicinetypearray = [[NSMutableArray alloc]initWithObjects:@"Capsules",@"Eyedrops", 
    @"Eardrops",@"Nosedrops",@"Inhaler",@"Syrup",@"Injections",@"Oils",@"Ointment", nil];
    medicationtypepicker.delegate = self;
    medicationtypepicker.showsSelectionIndicator = YES;
    medicationtypepicker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:medicationtypepicker];
   }

}

   -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
     [secondtextfield resignFirstResponder];
     return YES;
     }

enter image description here

if some one has idea to dismiss the keyboard. Please provide the solution.

Thankyou very much.


Solution

  • It sounds like the right solution for your problem is to set the first text field's inputView property to the picker view.

    Have your view controller create and configure the picker when the Medication Details screen is presented, and assign that picker to the first text field's inputView. Then, as the user moves from one field to another, the keyboard or picker will automatically be displayed without you having to fool around with resigning first responder and so forth.