【问题标题】:resignFirstResponder is not working as expectedresignFirstResponder 没有按预期工作
【发布时间】:2013-02-14 11:17:09
【问题描述】:

我有 uitextfield 在 uitableview 中输入国家/地区值。当点击该字段时,我正在显示加载了国家/地区值的表格视图。

当点击任何单元格时,它会返回到相同的表单并在国家字段中添加选定的值。我的意思是我将选定的值分配给 viewwillappear 方法内的文本字段。在国家字段内设置文本后。我想辞职键盘,所以我添加了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;
}

【问题讨论】:

    标签: iphone ios uitableview uitextfield


    【解决方案1】:

    使用下面的代码,希望对你有所帮助。

    - (BOOL)textFieldShouldReturn:(UITextField *)textField 
     {
    
      [self.view endEditing:YES];
    
      return YES;
    }
    

    确保您将代理连接到xib-file 中的fileowner。或在viewDidload 中使用以下代码。

    yourtextfield.delegate=self;
    

    并在您的 .h 文件中使用 <UITextfielddelegate>

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        改用这个怎么样:

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

        【讨论】:

          猜你喜欢
          • 2021-10-19
          • 2020-03-18
          • 2012-06-14
          • 2014-11-15
          • 1970-01-01
          • 2012-07-02
          • 2011-09-07
          • 2013-03-03
          • 2015-05-18
          相关资源
          最近更新 更多