ios iPhone 如何在打字时获取 UITextField 的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10363856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:08:30  来源:igfitidea点击:

iPhone how to get UITextField's text while typing?

iphoneobjective-ciosuitextfield

提问by Alex Stone

I'm trying to show changes made to a UITextFieldon a separate UILabel. Is there a way to capture full text of the UITextFieldafter each character the user types in? Currently I'm using this method, but it does not capture the last character that the user has entered.

我试图UITextField在单独的UILabel. 有没有办法捕获UITextField用户输入的每个字符后的全文?目前我正在使用这种方法,但它不会捕获用户输入的最后一个字符。

I know that UITextViewhas "didChange" method, but I could not find that method for a UITextField.

我知道UITextView有“didChange”方法,但我找不到UITextField.

//does not capture the last character

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

        [self updateTextLabelsWithText: textField.text];

    return YES;
}

How can I take text out of UITextField after each character entered?

输入每个字符后,如何从 UITextField 中取出文本?

Thank you!

谢谢!

回答by Mrunal

  1. First add one UITextFieldand UILabelto storyboard / nib
  2. Now assign IBOutletfor UILabel(Here I have used myLabel)
  3. Assign UITextFieldDelegateto file owner, also implement the same delegate in .h file
  4. Use these lines of code:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        [self updateTextLabelsWithText: newString];
    
        return YES;
    }
    
    -(void)updateTextLabelsWithText:(NSString *)string
    {
         [myLabel setText:string];
    }
    
  1. 首先添加一个UITextFieldUILabel到情节提要/笔尖
  2. 现在分配IBOutletUILabel(这里我用myLabel
  3. 分配UITextFieldDelegate给文件所有者,也在 .h 文件中实现相同的委托
  4. 使用这些代码行:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        [self updateTextLabelsWithText: newString];
    
        return YES;
    }
    
    -(void)updateTextLabelsWithText:(NSString *)string
    {
         [myLabel setText:string];
    }
    

Hope this helps.

希望这可以帮助。

回答by Fabs

Simply handle the "Editing Changed" event

只需处理“编辑更改”事件

[textField addTarget:self 
              action:@selector(editingChanged:)
    forControlEvents:UIControlEventEditingChanged];

and the selector:

和选择器:

-(void) editingChanged:(id)sender {
   // your code
}

You can do this manually, or with the storyboard by CTRL-Dragging the "Editing changed" Sent event to your .h, creating the editingChanged method for you.

您可以手动执行此操作,也可以通过按住 CTRL 键将“Editing changed” Sent 事件拖到您的 .h 中,为您创建 editChanged 方法来使用情节提要。

回答by ColossalChris

In Swift 2.0+

在 Swift 2.0+ 中

 class CheckInViewController: UIViewController, UITextFieldDelegate {

 override func viewDidLoad() {
    super.viewDidLoad()

    yourTextField.delegate = self

 }

 func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
    var updatedTextString : NSString = textField.text as NSString
    updatedTextString = updatedTextString.stringByReplacingCharactersInRange(range, withString: string)

    self.methodYouWantToCallWithUpdatedString(updatedTextString)
    return true
 }

}

Hope it helps you swift pioneers

希望能帮到各位快速的开拓者

回答by UnRewa

Swift with addTarget Swift 2.0+

Swift 与 addTarget Swift 2.0+

   titleTextField.addTarget(self, action: #selector(textFieldTyping), forControlEvents: .EditingChanged)

And implement selector

并实施 selector

func textFieldTyping(textField:UITextField)
{
    //Typing
}

回答by Dave Levy

In Swift 3.0:

在 Swift 3.0 中:

Some of these solutions were one character behind, or for earlier versions of Swift and Obj-C. Here is what I am using for Swift 3.0

其中一些解决方案落后于一个字符,或者用于早期版本的 Swift 和 Obj-C。这是我用于 Swift 3.0 的内容

In the class I declared a placeholder to store the text.

在类中,我声明了一个占位符来存储文本。

var tempName = ""

In ViewDidLoad I used:

在 ViewDidLoad 我使用:

nameField.addTarget(self, action: #selector(typingName), for: .editingChanged)

Then I made a function called:

然后我做了一个函数,叫做:

   func typingName(textField:UITextField){

        if let typedText = textField.text {
            tempName = typedText
            print(tempName)
        }
    }

回答by dsunku

Swift 3.0+: This worked very nicely for me.

Swift 3.0+:这对我来说非常有效。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    self.yourLabel.text = "\(textField.text ?? "")\(string)"
    return true
}

回答by Amul4608

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField == yourTextFieldOutletName
        {
           if yourTextFieldOutletName.isEmpty == false
           {
             youtlabelname.text = yourTextFieldOutletName.text!
            }


        }

        return true

    }

func textViewDidEndEditing(_ textView: UITextView) {
       if textField == yourTextFieldOutletName
            {
               if yourTextFieldOutletName.isEmpty == false
               {
                 youtlabelname.text = yourTextFieldOutletName.text!
                }


            } 
    }