Changing returnButton while editing freezes the keyboard for 1 click

I am creating a login screen for my application, and I want the return button to say “Next” when there is a field that is not yet complete, and it should say “Go” when all the fields are filled (I'm talking about UITextFields).

The code below works great in that it shows Next and Go at the right moments. But whenever it changes from "Next" to "Go", the next brief on the keyboard is ignored. When he says “Go,” and I clear the text box with a backward stroke, there is no such problem, and it shows “Next” as it should. It is almost like an old keyboard still there and disappears after listening.

My question is: what is the source of this problem and more importantly, how do I get rid of this keyboard freeze?

UITextField *theSender = (UITextField *)sender;
if (allTextFieldsAreFilled) {
    if (theSender.returnKeyType!=UIReturnKeyGo) {
        theSender.returnKeyType = UIReturnKeyGo;
        [theSender resignFirstResponder];
        [theSender becomeFirstResponder];
    }
} else {
    if (theSender.returnKeyType!=UIReturnKeyNext) {
        theSender.returnKeyType = UIReturnKeyNext;
        [theSender resignFirstResponder];
        [theSender becomeFirstResponder];
    }
}

This code is called every time the value of one of the three UITextFields changes, therefore it is an IBAction associated with the Editing Changed event.

Thanks in advance for your help!

EDIT

I found out that this will happen only if the text field is configured for protection (password). When it is not configured for protection, it will not freeze, and my code works fine! The problem is that changing the “Go” button usually occurs when the “protected” text field is a responder. So this does not change anything in this problem.

+5
source share
1 answer

Single View ; ViewController.m

#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>
{
    @private
    IBOutlet UITextField* m_fieldA;
    IBOutlet UITextField* m_fieldB;
    IBOutlet UITextField* m_fieldC;
}

@end

@implementation ViewController

// connected to Editing Did Begin
- (IBAction) onFocus:(UITextField*)_textField
{
    [self updateKeyboardFor:_textField];
}

// connected to Editing Changed
- (IBAction) onChanged:(UITextField*)_textField
{
    [self updateKeyboardFor:_textField];
}

- (void) updateKeyboardFor:(UITextField*)_textField
{
    bool allTextFieldsAreFilled = [m_fieldA.text length] && [m_fieldB.text length] && [m_fieldC.text length];

    if (allTextFieldsAreFilled)
    {
        if (_textField.returnKeyType != UIReturnKeyGo)
        {
            _textField.returnKeyType = UIReturnKeyGo;
            //[_textField resignFirstResponder];
            //[_textField becomeFirstResponder];
            [_textField reloadInputViews];
        }
    }
    else
    {
        if (_textField.returnKeyType != UIReturnKeyNext)
        {
            _textField.returnKeyType = UIReturnKeyNext;
            //[_textField resignFirstResponder];
            //[_textField becomeFirstResponder];
            [_textField reloadInputViews];
        }
    }
}

// A part of UITextFieldDelegate
- (BOOL) textFieldShouldReturn:(UITextField*)_textField
{
    if (_textField.returnKeyType == UIReturnKeyGo)
    {
        [_textField resignFirstResponder];

        // go off and perform 'go'
    }
    else
    {
        if(_textField == m_fieldA) [m_fieldB becomeFirstResponder];
        if(_textField == m_fieldB) [m_fieldC becomeFirstResponder];
        if(_textField == m_fieldC) [m_fieldA becomeFirstResponder];
    }

    return true;
}

@end

XIB UITextFields IBOutlets, IBActions, delegate .

, .

, - , ""; , . , .


. . , reloadInputViews .

, ; , , , / , , . iOS... , . , , , , .

+1

All Articles