2

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.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Nitin
  • 1,966
  • 4
  • 22
  • 53

3 Answers3

3

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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    110% spot on. This will replace the keyboard with the pickerView and therefore when that textfield is active the keyboard will be dismissed and your picker view will be active! – theiOSDude Dec 12 '11 at 12:15
1

Try telling the secondTextField to also resignFirstResponder after you determine that firstTextField is active:

 -(void)textFieldDidBeginEditing:(UITextField *)myTextField
  {  
     if(myTextField == firsttextfield)
     {
        [firsttextfield resignFirstResponder];
        [secondtextfield resignFirstResponder];
        medicationtypepicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,190,320,215)];
        ....
      }
   }  
Suz
  • 3,744
  • 3
  • 23
  • 26
0

check out that your text field's property " Did End on Exit" is connected with the 'Files Owner' proerly. Then use; -(void)textFieldDidBeginEditing:(UITextField *)myTextField
{ if(myTextField == firsttextfield)
{
...
[secondtextfield resignFirstResponder]; ..... } }

Rohan
  • 2,939
  • 5
  • 36
  • 65