IPhone, how to get UITextField text while typing?

I am trying to show the changes made to a UITextField on a separate UILabel . Is there a way to capture the full text of a UITextField after every character the user enters? I am currently using this method, but it does not capture the last character entered by the user.

I know that a UITextView has a didChange method, but I could not find this method for a 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 extract text from a UITextField after entering each character?

Thank!

+59
ios objective-c iphone uitextfield
Apr 28 2018-12-12T00:
source share
8 answers
  • First add one UITextField and UILabel to the / nib storyboard
  • Now assign an IBOutlet to UILabel (here I used myLabel)
  • Assign UITextFieldDelegate the file owner, also implement the same delegate in the .h file
  • 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]; } 

Hope this helps.

+137
Apr 28 2018-12-12T00:
source share

Just handle the Edit Edited event.

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

and selector:

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

You can do this manually or by using the CTRL storyboard-Drag and drop the "Edit Modified" Sent event onto your .h, creating a Changed editing method for you.

+68
Mar 23 '14 at 8:21
source share

In 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 this helps you fast pioneers.

+5
Nov 05 '15 at 17:27
source share

Swift with addTarget Swift 2.0 +

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

And we implement selector

 func textFieldTyping(textField:UITextField) { //Typing } 
+4
Apr 26 '16 at 8:27
source share

In Swift 3.0:

Some of these solutions were behind a single character, or for earlier versions of Swift and Obj-C. Here is what I use for Swift 3.0

In the class, I declared a placeholder for storing text.

 var tempName = "" 

In ViewDidLoad, I used:

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

Then I produced a function:

  func typingName(textField:UITextField){ if let typedText = textField.text { tempName = typedText print(tempName) } } 
+1
Jun 15 '17 at 20:35
source share

Swift 3. 0+: I really liked it.

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { self.yourLabel.text = "\(textField.text ?? "")\(string)" return true } 
0
Feb 20 '18 at 23:20
source share
 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! } } } 
0
Mar 07 '18 at 5:53
source share

Can someone share the complete code as an example for this newbie (me)?

0
Dec 18 '18 at 20:18
source share



All Articles