繁体   English   中英

如何在同一ViewController中为特定的UITextField禁用键盘?

[英]How do you disable keyboard for specific UITextField in same ViewController?

我在ViewController中有四个文本字段,并且想为其中两个(textField1和textField2)禁用键盘。

在尝试将文本字段分配为viewDidLoad中的委托后,我尝试实现以下方法

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField) {
    if ([self.textField1 isTouchInside] || [self.textField2 isTouchInside] {
        return NO;
    } else {  
        return YES;
    }
}

但是,这将完全禁用ViewController的键盘,因此在尝试编辑textField3和textField4时不会出现。 我该如何解决这个问题?

例如,是否有一种方法可以在结束textField编辑之后再次刷新运行的textFieldShouldBeginEditing方法?

另外,我知道我可以创建一个标签来完成类似的操作,但是在我的情况下,我更喜欢使用文本字段。

编辑:所以我省略了一个大细节。 按textField1和2时,我将触发IBaction。但是,Lootsch的回答给了我一个主意。

在textField1 / 2Pressed IB操作中,我运行了textfield.enable:NO方法,然后在触发第二个将数据提交到文本字段的操作时重新启用它们,如下所示

- (IBAction)textField1Pressed:(id)sender {
    self.textField.Enabled = NO;   
}

- (IBAction)submitToTextField1:(id)sender {
    self.textField.text = @"blah blah";
    self.textField.Enabled = YES;    
}

尽管这需要两次输入一个退出操作,但这对我有用。 另外,我不必使用此解决方案来操纵textFieldShouldBeginEditing方法。

您应该禁用这两个textField(通过代码或通过IB),或者可以禁用用户交互(外观不同,功能相同):

textField3.enabled = NO;
textField4.enabled = NO;

要么:

textField3.userInteractionEnabled = NO;
textField3.userInteractionEnabled = NO;

第二种方法不会更改UITextField的外观,而第一种方法则表明已禁用这些TextField。

应该做的事情

if (textField==textField1 || textField==textField2) [textField resignFirstResponder]; 

请尝试这个

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

if (textField==self.textField1 || textField==self.textField2)
{
    return NO;

}
    else
{
    return YES;
}

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)mytextField
{

    if((mytextField.tag == 104) || (mytextField.tag == 105) || (mytextField.tag == 106))
    {        
        if(mytextField.tag == 104)
        {
            point = 50;
//            if(textField1.tag == 4)
            {
                [self showDatePickerWithTitle:@"Select DOB"];
                return NO;
            }
           //

        }
     }
else
{
retutn YES;
}

//you can also try this piece of code.With tag property of textfield.And make sure your tag is not zero,because the default tag is 0 for the main view.

暂无
暂无

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

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