문제

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

도움이 되었습니까?

해결책

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!

다른 팁

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];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top