iPhone how to get UITextField's text while typing?

IphoneObjective CIosUitextfield

Iphone Problem Overview


I'm trying to show changes made to a UITextField on a separate UILabel. Is there a way to capture full text of the UITextField after 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.

I know that UITextView has "didChange" method, but I could not find that 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 take text out of UITextField after each character entered?

Thank you!

Iphone Solutions


Solution 1 - Iphone

  1. First add one UITextField and UILabel to storyboard / nib

  2. Now assign IBOutlet for UILabel (Here I have used myLabel)

  3. Assign UITextFieldDelegate to 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];
     }
    

Hope this helps.

Solution 2 - Iphone

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.

Solution 3 - Iphone

Swift with addTarget Swift 2.0+

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

And implement selector

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

Solution 4 - Iphone

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 it helps you swift pioneers

Solution 5 - Iphone

In 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

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

var tempName = ""

In ViewDidLoad I used:

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)
        }
    }

Solution 6 - Iphone

Swift 3.0+: This worked very nicely for me.

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

Solution 7 - Iphone

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!
                }
                

            } 
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex StoneView Question on Stackoverflow
Solution 1 - IphoneMrunalView Answer on Stackoverflow
Solution 2 - IphoneFabsView Answer on Stackoverflow
Solution 3 - IphoneUnRewaView Answer on Stackoverflow
Solution 4 - IphoneChris KlinglerView Answer on Stackoverflow
Solution 5 - IphoneDave LevyView Answer on Stackoverflow
Solution 6 - IphonedsunkuView Answer on Stackoverflow
Solution 7 - IphoneAmul4608View Answer on Stackoverflow