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,694
  • 13
  • 151
  • 195
Prooshani
  • 534
  • 7
  • 20

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,694
  • 13
  • 151
  • 195
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
  • 2,002
  • 1
  • 13
  • 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,765
  • 9
  • 65
  • 71
utahwithak
  • 6,235
  • 2
  • 40
  • 62