English 中文(简体)
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 textfield 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

}




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签