【问题标题】:Can't get keyboard to resign consistently with UITextView in UITableViewCell无法让键盘与 UITableViewCell 中的 UITextView 一致地辞职
【发布时间】:2009-12-05 16:04:24
【问题描述】:

我在 UITableView 单元格中有一个 UITextView。我一直无法让键盘在编辑后始终退出。检测 DidEndEditing 没有奏效。将我自己的“完成”按钮添加到工具栏会带来间歇性的结果。建议? (注意:这是 UITextView 而不是 UITextField。谢谢)

【问题讨论】:

  • 你能显示完成按钮的代码并将文本视图添加到单元格吗?
  • @JoeCannatti 我有类似的问题。我在顶部有一个保存按钮并使用 resignFirstResponder,但它不起作用。它只会从 UITextView 移除焦点(不隐藏键盘)。任何想法
  • 我通过向 didSelectRowAtIndexPath:(NSIndexPath *)indexPath 表中的每个部分添加 [myTextView resignFirstResponder] 消息解决了这个问题,但包含 UITextView 的部分除外。

标签: iphone uitableview uitextview


【解决方案1】:

您是否在完成编辑后关闭表格视图的控制器?我遇到了在执行[textView resignFirstResponder] 以及调用(类似于[self doneClicked:nil])时发生的非确定性崩溃,该调用将关闭托管 UITableView 的视图控制器。

它将释放 UITextView,当调用返回到发起 didEndEditing 调用的 UITextView 方法时,它会崩溃或行为不一致(因为视图已被释放)..

解决方案是延迟一段时间后调用所有内容:

[self performSelector:@selector(doneClicked:) withObject:nil afterDelay:0.5]

【讨论】:

  • 我不确定这是否适合我。我有一个超载的“保存”按钮的外观。当用户编辑 UITextView 时,该保存将关闭键盘并保存 UITextView 数据。在任何其他时间,“保存”按钮将调用不同的函数来保存和关闭。
【解决方案2】:

将文本视图添加到单元格:

cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
managedTextView = [[[UITextView alloc] initWithFrame:CGRectMake(7,8,260, 30)] autorelease];
managedTextView.delegate = self;
managedTextView.scrollEnabled = YES;
managedTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
managedTextView.text=thought.managedthought;
[cell.contentView addSubview: managedTextView];
cell.accessoryType = UITableViewCellAccessoryNone;

完成按钮代码:

- (void)saveTextView:(id)sender
{
    [managedTextView resignFirstResponder];
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save:)];
    self.navigationItem.rightBarButtonItem = saveButton;
    [saveButton release];
...
}

(保存整个 UITableViewController 时使用“新建”保存按钮)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    相关资源
    最近更新 更多