iPhone how to get UITextField's text while typing?
Solution 1:
- 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:
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:
Swift with addTarget Swift 2.0+
titleTextField.addTarget(self, action: #selector(textFieldTyping), forControlEvents: .EditingChanged)
And implement selector
func textFieldTyping(textField:UITextField)
{
//Typing
}