iPhone文本字段/键盘 - 如何在第一个文本字段完成之前禁用第二个文本字段

时间:2009-11-25 11:00:20

标签: iphone keyboard uitextfield

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

我想在第一个文本字段的键盘仍处于打开状态时禁用显示键盘的其他文本字段,如果这有意义的话?我知道有办法阻止文本字段的可编辑,但是如何在键盘打开的情况下判断它?

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

这是我的代码:

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 个答案:

答案 0 :(得分: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。

希望这会有所帮助。 感谢

答案 1 :(得分:0)

你可以制作一些标志,例如smth就像BOOL类型的needInfoHasBeenEntered并在textFieldShouldBeginEditing委托方法中为所有其他textFields返回

答案 2 :(得分:0)

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

禁用

上文本字段的userInteraction
- (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

}

答案 3 :(得分:0)

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

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