【问题标题】:How do you disable keyboard for specific UITextField in same ViewController?如何在同一个 ViewController 中禁用特定 UITextField 的键盘?
【发布时间】:2014-04-01 19:45:21
【问题描述】:

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

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

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

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

例如,有没有办法在 textField 上编辑结束后再次刷新 run 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 方法。

【问题讨论】:

    标签: ios iphone objective-c uiviewcontroller uitextfield


    【解决方案1】:

    您应该禁用这两个文本字段(在代码中或通过 IB),或者您可以禁用用户交互(不同的外观,相同的功能):

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

    或者:

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

    第二种方法不会改变 UITextFields 的外观,而第一种方法将表明这些 TextFields 被禁用。

    【讨论】:

      【解决方案2】:

      应该做的事情

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

      【讨论】:

        【解决方案3】:

        请试试这个

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

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

        }

        【讨论】:

          【解决方案4】:
          - (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.
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-11-11
            • 1970-01-01
            • 1970-01-01
            • 2017-10-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多