2

I know that if you use the following line of code, you can detect what the user has typed in a TEXT VIEW.

if ([[textView text] isEqualToString:@"")

I want to detect whether the user has typed anything in a text FIELD, and this does not work with text fields. What should I change? Thanks for your help!

Jack Humphries
  • 13,056
  • 14
  • 84
  • 125
  • 1
    You seem to be missing a colon and a bracket but besides that, why doesn't it work with a text field? – Joe Jul 14 '11 at 22:21
  • http://stackoverflow.com/questions/7010547/uitextfield-text-change-event will help you to detect changes – Dejell Apr 03 '14 at 17:41

3 Answers3

12

The UITextFieldDelegate is the preferred way to find this out.

- (void)textFieldDidBeginEditing:(UITextField *)textField - this will only tell you that it has become the first responder / key field, this does not guarantee that the user modified the value

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string - this method will tell you when ever the user hits a key to change the text, a "paste" will cause multiple characters to be in the string, backspace over one, or delete of a selection will cause the string to be empty, …

- (void)textFieldDidEndEditing:(UITextField *)textField - this is a common location to check the value in the field against the value in the model to see if an edit has occurred. this will only be called when the field relinquishes "key" status, note that a user could tap a button to cause the whole view to go away before this is called, often making it useful to track if any field you are interested in becomes the "key field" (see first method)

it can be useful to set break points in relevant implementations to familiarize yourself with their call order/logic

bshirley
  • 8,217
  • 1
  • 37
  • 43
  • What if I want to know if the user has ALREADY inserted one character to the field? textFieldDidEndEditing is being called only when leaving the field – Dejell Mar 11 '14 at 06:40
  • as mentioned above, you need to make note of that in the second method. – bshirley Apr 02 '14 at 17:20
  • That's not the correct way. http://stackoverflow.com/questions/7010547/uitextfield-text-change-event is the correct one to detect changes – Dejell Apr 03 '14 at 11:37
  • 1
    Either is "correct" (depending on the circumstances). I you need to be able to prevent an edit in the second method above, this solution is the one you want. If you will *never* prevent an edit, either solution is functional ("correct, if you will.) I would agree that the linked solution seems cleaner in the simple/common case. – bshirley May 04 '14 at 17:54
1

You can retrieve contents of a UITextField the same way you can a UITextView, by accessing the "text" property. The code below is if a text field is not empty.

  if (![someTextField.text isEqualToString:@""])
  {
        NSLog(@"someTextField is not empty");
  }

You can also use the UITextField delegate to detect when a user starts typing, stops typing, etc. More information about that is available in the documentation.

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/doc/uid/TP40006991

Finally, here's a link to the UITextField docs. https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextField_Class/Reference/UITextField.html#//apple_ref/doc/uid/TP40006888

Brandon Schlenker
  • 5,078
  • 1
  • 36
  • 58
  • I tried that and it didn't work. I forgot to unlink my text view (I was playing with that) and I was excited until I realized what happened. Then I relinked the text field and it didn't work... Also, the user could start typing and then delete everything... Thanks for trying though! – Jack Humphries Jul 14 '11 at 23:56
  • That's the correct and only way to grab to text from a UITextField. If it isn't working then there is an error somewhere on your end. – Brandon Schlenker Jul 15 '11 at 00:56
0

you can check for text length instead of content

if ([[textView text] length]==0)
ArunGJ
  • 2,685
  • 21
  • 27