繁体   English   中英

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

[英]iPhone textfields/keyboard - how to disable second textfield until first one has been completed

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

我想在第一个文本字段的键盘仍处于打开状态时,禁止其他文本字段显示键盘,是否有意义? 我知道有种方法可以阻止文本字段可编辑,但是如何知道何时已经打开键盘呢?

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

这是我的代码:

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;

}

谢谢!

我只希望您可以这样做

#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是一个非常糟糕的主意。 有许多解决方法可以实现您的目标。 尽量不要使用阻止用户界面。

希望这可以帮助。 谢谢

您可以创建一些标志,例如BOOL类型的mustthInfoInfoHasBeenEntered之类的标志,并在所有其他textField的textFieldShouldBeginEditing委托方法中将其返回

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

在以下位置禁用文本字段的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;

}

要么

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

}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM