Question

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....

Was it helpful?

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!

OTHER TIPS

1) Try making your code less error prone.

instead of this

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

do this

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

2) use textfield.tag property and set unique tags for these text fields. and do your stuff about become/resign first reponders by using

(uitextfield *)field[self viewWithTag: uniqueTag];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top