1

I am having a problem finding the length of a text field's text!

I searched here a lot and just found that it would work with textfield.text.length or something like that.
My problem is textfield.text is not working for me! If I write this code: textfield.text; then I get an error like this:

property "text" not found on object of type NSTextField

I have to find the length of an text field in order to limit the number of characters.

What should I do? Thanks in advance.

jscs
  • 63,095
  • 13
  • 148
  • 192
Prooshani
  • 514
  • 6
  • 19

3 Answers3

7

Most of the classes in AppKit, as opposed to UIKit, don't have properties. NSTextField doesn't even have a method called text. You need the stringValue method, which is inherited from NSControl

jscs
  • 63,095
  • 13
  • 148
  • 192
2

The generally accepted way of doing this (limiting the length of NSTextField) is by using the NSFormatter helper class. See Here

Community
  • 1
  • 1
nageeb
  • 1,992
  • 1
  • 12
  • 25
0

To limit the length you add the delegate methods to your controller by adding

< UITextFieldDelegate > 

in the header file. Then in the implementation add the following method:

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

In here just check the length and either return the string or just the string length you want.
You can get the length with:

[[textfield stringValue] length]

In interface builder be sure and add the file owner as the delegate to the text field

Anne
  • 26,375
  • 9
  • 63
  • 70
utahwithak
  • 6,115
  • 2
  • 39
  • 60