iPhone how to get UITextField's text while typing?
IphoneObjective CIosUitextfieldIphone 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
-
First add one
UITextField
andUILabel
to storyboard / nib -
Now assign
IBOutlet
forUILabel
(Here I have used myLabel) -
Assign
UITextFieldDelegate
to file owner, also implement the same delegate in .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.
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!
}
}
}