代码之家  ›  专栏  ›  技术社区  ›  RGriffiths

检测UITextfield中的文本更改

  •  22
  • RGriffiths  · 技术社区  · 13 年前

    我希望能够检测某些文本是否在 UITextField 这样我就可以启用 UIButton 以保存更改。

    7 回复  |  直到 11 年前
        1
  •  40
  •   DarkDust    11 年前

    而不是观察通知或实施 textField:shouldChangeCharacterInRange:replacementString: ,只需添加一个事件目标更容易:

    [textField addTarget:self
                  action:@selector(myTextFieldDidChange:)
        forControlEvents:UIControlEventEditingChanged];
    
    - (void)myTextFieldDidChange:(id)sender {
        // Handle change.
    }
    

    请注意,该事件是 UIControlEventEditingChanged UIControlEventValueChanged !

    与其他两种建议的解决方案相比,其优势在于:

    • 您不需要记住使用注销控制器 NSNotificationCenter .
    • 调用事件处理程序 之后 已经做出了改变,这意味着 textField.text 包含用户实际输入的文本。这个 text字段:shouldChangeCharacterInRange:替换字符串: 调用委托方法 之前 更改已经应用,因此 text字段.text 还没有给你用户刚刚输入的文本,你必须先自己应用更改。
        2
  •  36
  •   Shiva_iOS Aaron Golden    7 年前

    利用 UITextFieldTextDidChange 通知或在文本字段上设置委托,并注意 textField:shouldChangeCharactersInRange:replacementString .

    如果您想通过通知来观察更改,您需要在代码中使用类似的内容来注册 notification :

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:theTextField];
    

    此处的TextField是 UITextField 你想看的。然后,上面代码中self是其实例的类必须实现 textFieldDidChange ,就像这样:

    - (void)textFieldDidChange:(NSNotification *)notification {
        // Do whatever you like to respond to text changes here.
    }
    

    如果文本字段要比 observer ,那么你必须 注销 用于观察员的通知 dealloc 方法事实上,这样做是个好主意,即使文本字段的寿命没有超过观察者。

    - (void)dealloc {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        // Other dealloc work
    }
    
        3
  •  9
  •   Community Mohan Dere    9 年前

    为此,首先需要为您的文本字段分配委托引用。delgate最好是vew控制器,它是视图的文件所有者。 是这样的

    myTextField.delegate = myViewControllerReferenceVariable
    

    在您的viewController界面中,告诉您将通过以下方式实现UITextFieldDelegate

    @interface MyViewController:UIViewController<UITextFieldDelegate>
    

    在您的视图中,控制器实现覆盖

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    

    所以代码看起来像

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
         {
            text = [textfield.text stringByReplacingCharactersInRange:range withString:string];
            if (textfield == refToTextFieldYouWantToCheck) {
                if ( ! [textToCheck isEqualToString:text] ) {
                   [theButtonRef setEnabled:YES];
                } 
             }
               return YES; //If you don't your textfield won't get any text in it
          }
    

    你也可以订阅通知,这有点混乱 你可以找到怎么做 here .

        4
  •  3
  •   Sunny    9 年前

    斯威夫特3.0

    流程1

    创建UITextfiled的IBOutlet并将Target添加到文本字段。

     m_lblTxt.addTarget(self, action: #selector(self.textFieldDidChange), for: UIControlEvents.editingChanged)
    
     func textFieldDidChange(textField:UITextField)
     {
         NSLog(textField.text!)
     }
    

    流程2

     m_lblTxt.delegate = self
    
     //MARK: - TextField Delegates
     func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 
     {
          print(textField.text!)
          return true
     }
    
        5
  •  2
  •   Mark Suman    8 年前

    这可以在 Editing Changed 的事件 UITextField 。从中拖动到您的代码中,然后创建 IBAction .

    例如:

    @IBAction func textFieldChanged(_ sender: UITextField) {
      print(sender.text)
    }
    

    此事件与此处其他答案中描述的相同,因为 .text 属性包含触发时更新的文本输入。这可以帮助清理代码混乱,因为不必以编程方式将事件添加到 输入框 在视图中。

        6
  •  1
  •   Firas    13 年前

    您可以创建一个变量来存储原始字符串,然后向通知中心注册以接收 UITextFieldTextDidChangeNotification 事件:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateButton:) name:UITextFieldTextDidChangeNotification object:nil];
    

    然后,创建一个方法来接收通知,并将文本字段的当前值与原始值进行比较

    -(void) updateButton:(NSNotification *)notification {
           self.myButton.enabled = ![self.myTextField.text isEqualToString:originalString];
    }
    

    当视图控制器被解除分配时,不要忘记取消注册通知。

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    
        7
  •  -3
  •   Alex Cio Olie    11 年前

    你可以添加这样的类成员 NSString *changeTemp 然后

    changetemp = textfield;
    
    if( change temp != textfild ){
        changetemp=textfild;
        NSLog(@" text is changed"
    } else { 
        NSLog(@" text isn't changed"):
    }