iPhone textfields/keyboard - how to disable second textfield until first one has been completed
I have a login screen with two textfields (username and password). When the user clicks on either textfield, the keyboard appears and everything is fine. However, if the user clicks on the other textfield before clicking Done (on the keyboard) for the first textfield, the text for the username isn't saved. The only way to save the original textfield text is by clicking back on it and selecting Done then.
I would like to disable the other textfield from displaying the keyboard while the first 开发者_如何学Gotextfield's keyboard is still open, if that makes sense? I know there's ways to stop textfields from being editable but how can I tell when there is already a keyboard open?
Another solution may be to disable the keyboard whenever the user clicks anywhere outside of the textfield? How would I do this?
Here's my code:
textFieldEmail = [[UITextField alloc] initWithFrame:frame];
textFieldEmail.keyboardType = UIKeyboardTypeEmailAddress;
textFieldEmail.returnKeyType = UIReturnKeyDone;
textFieldEmail.tag = 0;
textFieldEmail.delegate = [[UIApplication sharedApplication] delegate];
textFieldPassword = [[UITextField alloc] initWithFrame:frame];
textFieldPassword.keyboardType = UIKeyboardTypeEmailAddress;
textFieldPassword.returnKeyType = UIReturnKeyDone;
textFieldPassword.tag = 1;
textFieldPassword.delegate = [[UIApplication sharedApplication] delegate];
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Thanks!
I you want this only you can do this like..
#pragma mark -
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField == textFieldEmail){
[textFieldPassword setUserInteractionEnabled:NO];
}
else if(textField == textFieldPassword)
[textFieldEmail setUserInteractionEnabled:NO];
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textFieldEmail setUserInteractionEnabled:YES];
[textFieldPassword setUserInteractionEnabled:YES];
[textField resignFirstResponder];
return YES;
}
But one thing I want to add, blocking UI is a very bad idea in developing iPhone apps. There are many workarounds to achieve your goal. Try not to use the blocking UI.
Hope this helps. Thanks
you can make some flag, for example smth like neededInfoHasBeenEntered of BOOL type and return it in your textFieldShouldBeginEditing
delegate method for all other textFields
I got the same problem and I tried the above code and it was working fine. This code is very much helpful for me. I have 4 textFields in the my view and I used the code as:
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if(textField == eventTextField){
[eventPlaceTextField setUserInteractionEnabled:NO];
[wineryTitleLabel setUserInteractionEnabled:NO];
[vintageTextField setUserInteractionEnabled:NO];
}
else if(textField == eventPlaceTextField)
{
[wineryTitleLabel setUserInteractionEnabled:NO];
[vintageTextField setUserInteractionEnabled:NO];
}
else if(textField == wineryTitleLabel)
{
[vintageTextField setUserInteractionEnabled:NO];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
int vintageVal = [vintageTextField.text intValue];
if(textField == eventTextField)
{
event.eventName = eventTextField.text;
[eventTextField setUserInteractionEnabled:YES];
[eventPlaceTextField becomeFirstResponder];
[textField resignFirstResponder];
}
else if(textField == eventPlaceTextField)
{
event.eventPlace = eventPlaceTextField.text;
[eventPlaceTextField setUserInteractionEnabled:YES];
[wineryTitleLabel becomeFirstResponder];
[textField resignFirstResponder];
}
else if(textField == wineryTitleLabel)
{
event.eventWinery = wineryTitleLabel.text;
[wineryTitleLabel setUserInteractionEnabled:YES];
[vintageTextField becomeFirstResponder];
[textField resignFirstResponder];
}
else if(textField == vintageTextField)
{
if([vintageTextField.text length] == 4 || [vintageTextField.text length]==0)
{
event.eventVintage = vintageVal;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"!!!WARNING!!!" message:@"Enter the Vintage in the format 'YYYY'"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return NO;
}
[vintageTextField setUserInteractionEnabled:YES];
[self setViewMovedUp:NO];
[textField resignFirstResponder];
}
return YES;
}
You can approach this on two different ways :
Disable the userInteraction of the textfield on
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textfield == textFieldEmail)
{
textFieldPassword.userInteractionEnabled = NO;
}else if(textfield == textFieldPassword){
textFieldEmail.userInteractionEnabled = NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
textFieldPassword.userInteractionEnabled = YES;
textFieldEmail.userInteractionEnabled = YES;
}
OR
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
UITextField *theOtherTextField;
if(textView == textFieldEmail)
{
theOtherTextField = textFieldPassword;
}
else if(textfield == textFieldPassword)
{
theOtherTextField = textFieldEmail;
}
return ![theOtherTextField isFirstResponder];
//Return no if the other text field is the first responder
}
精彩评论