Search code examples
objective-ciosios5uitextfieldbecomefirstresponder

UITextView won't resignFirstResponder so another can becomeFirstResponder


I have a toolbar in my UITextfields' inputAccessoryView. If the 'next' button is hit it makes the next textfield in an orderedSet of all my textfields become the first responder. THAT works perfectly.

BUT I can't get the 'previous' textfield to becomeFirstResponder.

I've checked in the console and the textfield does call textFieldShouldBeginEditing and I am returning YES but it never calls textFieldDidBeginEditing and the textfield that should resignFirstResponder never calls textFieldShouldEndEditing.

So the textfield is getting the message to becomeFirstResponder but doesn't.

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

The weirdest thing is that if I force the textfield to resignFirstResponder by some action that just does that, the 'previous' textfield suddenly becomesFirstResponder. Like it was being added to a list of responders and it became its turn....


Solution

  • And as is often the case when strange things happen, it was unrelated to becoming first responder. I was accidentally changing my activeTextfield pointer to nil in the middle of a bunch of logic by way of the textfield delegate. ^o^

    Disregard!