1

我已经在我的应用程序中的 UITextFields 上实现了标准的“下一个”功能(按下一个会将您移动到下一个适当的文本字段)。大多数情况下,这很有效,并且会移动到适当的文本字段没有问题。但是在某些情况下,由于某种原因,当我要求它成为第一响应者时,textField 会返回 NO,我不知道为什么。

有关文本字段/视图的一些信息:

该视图有多个 UITableViews,其中一些 UITableViewCells 具有我想成为其中的第一响应者的 UITextfields,其他人则没有。创建表后,我会遍历主视图子视图以查找所有第一响应者并将它们添加到第一响应者数组中(_firstResponders - 变量名)。当需要下一个响应者时,每个单元格都通过委托回调到主视图。

下面是我用来获取下一个响应者的代码:

//get the current first responders index
NSInteger currentIndex = [_firstResponders indexOfObject:currentResponder];

// if the current index plus 1 is less than the overall count of first responders on this view then continue, else resign the responder as no more are avilable
if((currentIndex+1) < [_firstResponders count])
{
    // get the potential next responder
    UIResponder *nextResponder = [_firstResponders objectAtIndex:currentIndex+1];  
    // if it can become the first responder assign it, 
    // else callback into this function with the unavilable first responder
   if([nextResponder canBecomeFirstResponder])
   {
     [currentResponder resignFirstResponder];
        BOOL becomeFirstResponder = [nextResponder becomeFirstResponder];

        if(becomeFirstResponder == NO)
        {
            DLog(@"TextField at index: %d has returned no to becoming first responder", (currentIndex+1));
        }
    }
    else
        [self getNextResponder:nextResponder];
}
else
    [currentResponder resignFirstResponder];
}

如您所见,我检查了它是否可以成为代码中的第一响应者,并且我还检查了是否手动启用了文本字段(我还在将响应者添加到响应者数组时检查了这一点)。还值得一提的是,我已经检查了数组大小和发生这种情况时要设置的响应者,它们是正确/有效的。

对我来说最奇怪的部分是,如果我在设置新的响应者之前没有在当前的响应者上调用 resignFirstResponder ,我之前只看到过这个。

有谁知道它返回 NO/我如何解决这个问题的原因?

提前致谢!

4

0 回答 0