Getting the length of an NSTextField's text property
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.
textfield.text
is not working for me!
If I write this code: textfield.text;
then I get an error like th开发者_开发技巧is:
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.
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
The generally accepted way of doing this (limiting the length of NSTextField) is by using the NSFormatter helper class. See Here
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
精彩评论