0

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

Will Larche
  • 3,119
  • 7
  • 26
  • 34

2 Answers2

0

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!

Will Larche
  • 3,119
  • 7
  • 26
  • 34
-2

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];
Nitin Alabur
  • 5,812
  • 1
  • 34
  • 52
  • Thanks but there is a lot going on that makes just using the tags impossible. This table is dynamic. Besides, the proper textfield is indeed receiving the becomeFirstResponder message. – Will Larche Jul 20 '12 at 19:02
  • are you sure, only your textfield of interest is getting the becomeFR message? could it be possible that after teh proper one, another one is getting the same message too? – Nitin Alabur Jul 20 '12 at 19:19
  • Well I'm watching shouldBecomeBeginEditing and it only gets called once. So I don't thing another one is getting the same message. I think the one that is firstResponder won't give it up. – Will Larche Jul 20 '12 at 20:02
  • so when you say "So the textfield is getting the message to becomeFirstResponder but doesn't." do you mean, you are not seeing the cursor in it? or some other textfield is still the first responder? – Nitin Alabur Jul 20 '12 at 21:07
  • some other textfield is still the firstResponder. It never resigns. But when it does from some different action, the new one becomes firstResponder like it was waiting. – Will Larche Jul 20 '12 at 22:00
  • is it possible for you to have a property UITextField *currentFirstResponder in that class? If so, you can always [currentFirstResponder resignFR] and before making the other field first responder. – Nitin Alabur Jul 20 '12 at 22:03
  • I do have an activeTextfield property but if I call that first doesn't seem to help. – Will Larche Jul 20 '12 at 22:36
  • 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^ – Will Larche Jul 23 '12 at 06:37
  • Good to know adding parentheses makes my code more robust. LOL. LET’S ADD MORE PARENTHESES EVERYWHERE! – mxcl Aug 07 '14 at 23:36