UITextView 不会 resignFirstResponder 所以另一个可以成为FirstResponder

Posted

技术标签:

【中文标题】UITextView 不会 resignFirstResponder 所以另一个可以成为FirstResponder【英文标题】:UITextView won't resignFirstResponder so another can becomeFirstResponder 【发布时间】:2012-07-20 18:41:48 【问题描述】:

我的 UITextfields 的 inputAccessoryView 中有一个工具栏。如果点击“下一个”按钮,它会使我所有文本字段的有序集中的下一个文本字段成为第一响应者。效果很好。

但我无法让“上一个”文本字段成为FirstResponder。

我在控制台中进行了检查,文本字段确实调用了 textFieldShouldBeginEditing,我返回的是 YES,但它从不调用 textFieldDidBeginEditing,并且应该 resignFirstResponder 的文本字段从不调用 textFieldShouldEndEditing。

所以文本字段正在获取成为成为FirstResponder 的消息,但没有。

- (IBAction)keyboardBarButtonDidTouch:(UIBarButtonItem *)sender

    if (sender==self.previousBarButton&&self.activeTextFieldArrayIndex.intValue>0)
    
        [(UITextField *)[self.textfields objectAtIndex:(self.activeTextFieldArrayIndex.intValue-1)] becomeFirstResponder];
    
    if (sender==self.nextBarButton&&self.activeTextFieldArrayIndex.intValue<(self.textfields.count-1))
    
        [(UITextField *)[self.textfields objectAtIndex:(self.activeTextFieldArrayIndex.intValue+1)] becomeFirstResponder];
    
    if (sender==self.doneBarButton)
    
        [self.activeTextField resignFirstResponder];
    

最奇怪的是,如果我通过某种操作强制文本字段 resignFirstResponder,那么“以前的”文本字段会突然变成 FirstResponder。就像它被添加到响应者列表中一样,轮到它了....

【问题讨论】:

【参考方案1】:

当奇怪的事情发生时,通常情况下,这与成为第一响应者无关。我不小心通过文本字段委托在一堆逻辑中间将我的 activeTextfield 指针更改为 nil。 ^o^

无视!

【讨论】:

【参考方案2】:

1) 尝试使您的代码不易出错。

而不是这个

if(sender==self.previousBarButton&&self.activeTextFieldArrayIndex.intValue>0)

这样做

if ((sender==self.previousBarButton) && (self.activeTextFieldArrayIndex.intValue>0))

2) 使用 textfield.tag 属性并为这些文本字段设置唯一标签。并使用

来做关于成为/辞职第一响应者的事情
(uitextfield *)field[self viewWithTag: uniqueTag];

【讨论】:

谢谢,但是有很多事情使得仅仅使用标签是不可能的。该表是动态的。此外,正确的文本字段确实收到了 becomeFirstResponder 消息。 您确定,只有您感兴趣的文本字段会收到 becomeFR 消息吗?有没有可能在正确的一个之后,另一个也收到相同的消息? 好吧,我在看 shouldBecomeBeginEditing ,它只被调用一次。所以我不认为另一个人收到同样的信息。我认为 firstResponder 不会放弃。 所以当你说“所以文本字段正在获取消息成为FirstResponder 但没有。”你的意思是,你没有看到光标?还是其他一些文本字段仍然是第一响应者? 其他一些文本字段仍然是 firstResponder。它从不辞职。但是,当它通过一些不同的操作执行时,新的操作会变成 firstResponder ,就像它在等待一样。

以上是关于UITextView 不会 resignFirstResponder 所以另一个可以成为FirstResponder的主要内容,如果未能解决你的问题,请参考以下文章