Iphone 辞职的FirstResponder未按预期工作

Iphone 辞职的FirstResponder未按预期工作,iphone,ios,uitableview,uitextfield,Iphone,Ios,Uitableview,Uitextfield,我使用uitextfield在uitableview中输入国家值。当点击该字段时,我将显示加载了国家/地区值的tableview 当点击任何单元格时,它返回到相同的形式,并将选定的值添加到“国家”字段中。我的意思是,我将所选的值指定给ViewWillAspect方法中的textfield。在国家/地区字段内设置文本后。我想退出键盘,所以我添加了resignFirstResponder,但它并没有退出键盘 下面是我的代码: - (void)textFieldDidBeginEditing:(UI

我使用uitextfield在uitableview中输入国家值。当点击该字段时,我将显示加载了国家/地区值的tableview

当点击任何单元格时,它返回到相同的形式,并将选定的值添加到“国家”字段中。我的意思是,我将所选的值指定给ViewWillAspect方法中的textfield。在国家/地区字段内设置文本后。我想退出键盘,所以我添加了resignFirstResponder,但它并没有退出键盘

下面是我的代码:

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

希望在添加UITextField委托时以下代码能够正常工作

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

  [self.myCountryField resignFirstResponder];

  return YES;
}

改用这个怎么样:

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

使用下面的代码,希望它能帮助你

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

  [self.view endEditing:YES];

  return YES;
}
确保您将代表连接到
xib文件中的
fileowner
。或者在
viewDidload
中使用以下代码

yourtextfield.delegate=self;
在.h文件中使用