UITextfield becomeFirstResponder returns NO

冷暖自知 提交于 2019-12-13 01:34:00

问题


I have implemented the standard 'next' feature on UITextFields in my app (pressing next will move you to the next appropriate text field). Most of the time this works well and will move to the appropriate textfield no problem. However on a few occasions for some reason the textField will return NO when i ask it to become the first responder and I am not sure why.

Some information about the textfields/view:

The view has multiple UITableViews, some of the UITableViewCells in these have the UITextfields i want to become first responder in them, others do not. When the tables are created i loop through the main views subviews to find all the first responders and add them to an array of first responders (_firstResponders - variable name). Each cell calls back via a delegate to the main view when the next responder is wanted.

Below is the code i use to get the next responder:

//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];
}

As you can see i check that it is available to become the first responder in code and i have also checked that the textfield is enabled manually (I also check this at the time of adding the responder to the array of responders). It is also worth mentioning i have checked the array size and the responder to be set when this occurs and they are correct/valid.

The strangest part for me about this is that I had only previously seen this if I did not call resignFirstResponder on the current responder before setting the new responder.

Does anyone know the reason for it returning NO/how i can fix this?

Thanks in advance!

来源:https://stackoverflow.com/questions/14851633/uitextfield-becomefirstresponder-returns-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!