0
votes

I am having the uitextfield to input country value inside the uitableview. When tapping on that field I am showing the tableview with country values loaded.

When tapping on any cell, it comes back to the same form and adds the selected value inside the country field. I mean I assign the selected value to the textfield inside the viewwillappear method. After setting the text inside the country field. I want to resign the keyboard, so I added resignFirstResponder, but it is not resigning the keyboard.

Below is my code:

 - (void)textFieldDidBeginEditing:(UITextField *)textField;
    {
      if( textField == self.myCountryField ){

        aCountryListView = 
            [[LCCountryListViewController alloc]init]; 

        UINavigationController *navigationController = 
            [[UINavigationController alloc] 
             initWithRootViewController:aCountryListView];

        [self presentModalViewController:navigationController animated:YES];

      }
    }
-(void)viewWillAppear:(BOOL)animated{
//-----------------------------------

  self.myCountryField.text = aCountryListView.mySelectedCountry;

  [self.myCountryField resignFirstResponder];

  [[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(keyboardWillShow:) 
    name:UIKeyboardWillShowNotification object:nil];

  [[NSNotificationCenter defaultCenter] addObserver:self 
  selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath 
//-------------------------------------------------------------------------------
{  
  static NSString *CellIdentifier = @"PoetNameCell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil) 
  {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

   cell.backgroundColor = [UIColor clearColor];

    if( indexPath.row == 0 ){

      UILabel *aLblTitle = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 220, 30)];
      aLblTitle.backgroundColor = [UIColor clearColor];
      aLblTitle.font = [UIFont fontWithName:@"Palatino-Bold" size:16.0];
      aLblTitle.text = NSLocalizedString(@"Country","Country");
      aLblTitle.textColor = [UIColor whiteColor];
      [cell.contentView addSubview:aLblTitle];

      myCountryField = [[UITextField alloc]initWithFrame:CGRectMake(133,5,150,30)];
      myCountryField.backgroundColor = [UIColor whiteColor];
      myCountryField.delegate = self;
      myCountryField.borderStyle = UITextBorderStyleRoundedRect;

      [cell.contentView addSubview:myCountryField];
    }
  return cell;
}
3

3 Answers

1
votes

use the below code, hope it will help you out.

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
 {

  [self.view endEditing:YES];

  return YES;
}

ensure that you will connect your delegate to fileowner in xib-file. or in viewDidload use the following code.

yourtextfield.delegate=self;

and in your .h file use <UITextfielddelegate>

0
votes

Hope the following code will work when add UITextField delegate

 - (BOOL)textFieldShouldReturn:(UITextField *)textField 
 {

  [self.myCountryField resignFirstResponder];

  return YES;
}
0
votes

How about using this instead:

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
                                           to:nil
                                         from:nil
                                     forEvent:nil];