FirstTimer
FirstTimer

Reputation: 313

How to change Keyboard's return type,iPhone

I have 2 textfields.I am checking when the both textfields are filled, than, i change the keyboard return button to "Go".Else, it will remain as "Return". Its working fine, the keyboard's return type changes,only, when i tap on textfield. I want, to change it automatically, once both the textfields are non-empty.

I tried putting my code

if ([txtEmail.text length] > 0 && [txtPass.text length] > 0) {
    // Set the return key here, e.g:
    txtEmail.returnKeyType = UIReturnKeyGo;
    txtPass.returnKeyType = UIReturnKeyGo;
}
else {
    // Set the return key here, e.g: 
    txtEmail.returnKeyType = UIReturnKeyDefault;
    txtPass.returnKeyType = UIReturnKeyDefault;
}

at

- (void)textFieldDidBeginEditing:(UITextField *)textField{}

but its not working for me.

Any idea, how can i change the return button from return to Go, when both he fields are non empty,automatically, user don't have tap on any text field to change it.

Upvotes: 1

Views: 1202

Answers (4)

kiran
kiran

Reputation: 4409

check in your header file UITextFieldDelegate protocal and

do not forget to set delegate to self.

txtEmail.delegate = self;
txtPass.delegate = self;

Upvotes: 1

Madhumitha
Madhumitha

Reputation: 3824

Set delegate for both textfields.then put your code in viewDidLoad once after creating reference of UItextFields

if ([txtEmail.text length] > 0 && [txtPass.text length] > 0) 
{
txtEmail.returnKeyType = UIReturnKeyGo;
txtPass.returnKeyType = UIReturnKeyGo;
}
else 
{

txtEmail.returnKeyType = UIReturnKeyDefault;
txtPass.returnKeyType = UIReturnKeyDefault;
}

Upvotes: 2

Abhishek
Abhishek

Reputation: 2253

//have you set the delegate of your textField if not then please set it.. 

txtEmail.delegate = self;
txtPass.delegate = self;

Upvotes: 1

poncha
poncha

Reputation: 7866

textFieldDidBeginEditting fires before editing started, so obviously the text does not change there.

What you need to do is to detect key press to discover changes and based on that change returnKey type. See this question: How can I detect a KeyPress event in UITextField? - it deals with key press events.

Upvotes: 1

Related Questions