繁体   English   中英

单击另一个UITextView时结束编辑

[英]End editing when clicking on another UITextView

我有一个带有多个海关单元格的UITableViewController,每个海关单元格都包含一个UITextView。 我想这样,如果用户正在编辑单元格A,然后单击单元格B,则单元格A结束编辑,但单元格B 开始编辑。

现在,我有以下手势识别器,当在tableView外部单击时,该操作结束了单元格的编辑:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard)];
[tap setCancelsTouchesInView:NO];
[self.view addGestureRecognizer:tap];

但是,当单击另一个UITextView时,此textview之后会调用shouldBeginEditing ,因此将其忽略。

我尝试使用textViewShouldBeginEditing / textViewShouldEndEditing函数,但没有成功。 我不知道我应该遵循这个方向还是创建具有自定义关联动作的另一个手势识别器?

您需要进行很多设置才能使其正常运行。

您的所有UITextField都需要设置其委托。 您需要跟踪UITextField当前是否正在编辑。 如果是这样,则需要调用resignFirstResponder否则将让您新近点击的UITextField开始编辑。

代码看起来像这样。

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool 
{
    if self.currentTextField == nil 
    {
      self.currentTextField = textField
      return true 
    } 
    else 
    {
      self.currentTextField!.resignFirstResponder()
      self.currentTextField = nil
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM