简体   繁体   English

操作方法:当UITextView不在视图时隐藏键盘?

[英]How To: hide keyboard when UITextView is out of view?

I've put some UITextViews in a UITableView(Controller) with custom cells, and I'm running into a problem. 我把一些UITextViews放在带有自定义单元格的UITableView(Controller)中,我遇到了一个问题。 The user can hit the "Edit" button up in the navbar to set the table into editing mode - each custom cell has its own way of enabling its individual UITextViews for text entry. 用户可以在导航栏中点击“编辑”按钮将表格设置为编辑模式 - 每个自定义单元格都有自己的方式启用其各个UITextViews进行文本输入。 That works fine. 这很好。

The problem I'm having is that if focus is on a text view that gets scrolled out of view when the user hits "Done", the keyboard remains and the textview remains in edit mode. 我遇到的问题是,如果焦点在文本视图上,当用户点击“完成”时滚动到视图外,键盘仍然保留,文本视图保持编辑模式。 This doesn't happen if the textview is in view. 如果文本视图在视图中,则不会发生这种情况。 More specifically - the cell i'm looking at is at the top of the screen and will act funny if it's scrolled above the iPhone screen. 更具体地说 - 我正在看的单元格位于屏幕的顶部,如果它在iPhone屏幕上方滚动,它将表现得很有趣。 I don't seem to have the same problem when the textview at the bottom of the table gets scrolled out. 当表格底部的textview滚动出来时,我似乎没有同样的问题。

I've done just about every single permutation I could think of to get the view to resign first responder, but it appears to me that the hidden textview doesn't get/send any messages (even any delegate methods) until it is back on to the screen OR some other text view gets focus. 我已经完成了我能想到的每一个排列,以便让第一响应者辞职,但在我看来,隐藏的textview不会获取/发送任何消息(甚至任何委托方法),直到它重新开启到屏幕或其他一些文本视图获得焦点。

What am I missing? 我错过了什么?

After working on this for the better part of a full day, this is what I learned: 在这一天的大部分工作中,这是我学到的:

  1. You can't actually access out-of-view cells in UITableView. 您实际上无法访问UITableView中的视图外单元格。 I guess that makes sense, though frustrating in my situation 我想这有道理,虽然令我感到沮丧
  2. No amount of redrawing or trying to manually resign first responder is going to help, even when you point to a specific cell in a specific row. 即使您指向特定行中的特定单元格,也不会重绘或尝试手动重新设置第一响应者的数量。
  3. setEditing: animated: (called when you hit the "Done" button) isn't the only thing going on setEditing:animated :(当你点击“完成”按钮时调用)不是唯一发生的事情
  4. I'm going to guess that the reason the UITextView remains in edit mode is because the cached version of the cell is in edit mode and cached cells/data aren't called by these various table methods (like setEditing:animated:) 我猜想UITextView保持编辑模式的原因是因为单元的缓存版本处于编辑模式,并且这些各种表方法都没有调用缓存的单元格/数据(比如setEditing:animated :)

This is the solution I found: (at UISearchBar and resignFirstResponder ): 这是我找到的解决方案:(在UISearchBar和resignFirstResponder ):
* calling [self.tableView endEditing:YES] causes all views to resign first responder, which means my UITextView is no longer selected. *调用[self.tableView endEditing:YES]会导致所有视图重新[self.tableView endEditing:YES]第一个响应者,这意味着我的UITextView不再被选中。 I'm not sure why it works this way, but appears setEditing: is sent to each cell & redrawn accordingly. 我不确定它为什么会这样工作,但是出现setEditing:被发送到每个单元格并相应地重新绘制。

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

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