tags:

views:

35

answers:

2

Hi,

i need to get text from a textfield when user presses any key. i am trying to use notification UITextFieldTextDidChangeNotification to get the text but not able to succeed. Can someone please guide me how to get the text from texfield on change. I have the textfields inside a uitableview. Thanks in advance.

A: 

you could use

- (void)textFieldDidBeginEditing:(UITextField *)textField

for when they start editing, or this

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

for every time they hit a key on the keyboard, or

- (void)textFieldDidEndEditing:(UITextField *)textField

for when the textviews first responder is resigned.

You can keep track of individual textfields by assigning tags when you populate the tableview, then you will know which text field is being changed.

AtomRiot
oh, and of course you will need to extend the UITextFieldDelegate to listen to those and set the delegate of the text fields.
AtomRiot
hi atom, thankx for replying. I have around 20 textboxes in uitableview, so when the user fills a textbox and scrolls down, the earlier textboxes are removed and i donot get the textboxes as i try to get the text from last textbox in - (void)textFieldDidEndEditing:(UITextField *)textField
pankaj
A: 

The easiest means is to use AtomRiots answer. After all, getting the text out of textfield is what the text field delegate is for. You can set a different delegate object for each textfield in you interface even those in tables.

If for some reason you wanted an arbitrary object to handle the text change you would register the object (self) for a the notification.

If you want to listen for all UITextFieldTextDidChangeNotification from all text fields:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleTextDidChange:)
                                             name:UITextFieldTextDidChangeNotification 
                                           object:nil];

If you want to listen for all notifications for a particular field

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleTextDidChange:)
                                             name:UITextFieldTextDidChangeNotification
                                           object:aPointerToATextFieldObj];
TechZen