1

我有一个带有两个文本字段(用户名和密码)的登录屏幕。当用户单击任一文本字段时,会出现键盘并且一切正常。但是,如果用户在单击第一个文本字段的完成(在键盘上)之前单击另一个文本字段,则不会保存用户名的文本。保存原始文本字段文本的唯一方法是单击它并选择完成然后。

我想在第一个文本字段的键盘仍然打开时禁用另一个文本字段显示键盘,如果这有意义吗?我知道有办法阻止文本字段可编辑,但我怎么知道什么时候已经打开了键盘?

另一种解决方案可能是在用户单击文本字段之外的任何位置时禁用键盘?我该怎么做?

这是我的代码:

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;

}

谢谢!

4

4 回答 4

1

我你想要这个只有你可以这样做..

#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;
}

但我想补充一点,在开发 iPhone 应用程序时,阻止 UI 是一个非常糟糕的主意。有许多解决方法可以实现您的目标。尽量不要使用阻塞的 UI。

希望这可以帮助。谢谢

于 2009-12-19T05:01:40.463 回答
0

您可以通过两种不同的方式来解决这个问题:

禁用文本字段的用户交互

- (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;

}

或者

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

}
于 2009-11-25T18:46:36.067 回答
0

您可以制作一些标志,例如 BOOL 类型的 requiredInfoHasBeenEntered 之类的,并在您的textFieldShouldBeginEditing委托方法中为所有其他文本字段返回它

于 2009-11-25T11:43:42.207 回答
0

我遇到了同样的问题,我尝试了上面的代码,它工作正常。这段代码对我很有帮助。我的视图中有 4 个文本字段,我将代码用作:

- (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;
}
于 2010-01-28T06:22:05.793 回答