简体   繁体   中英

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.
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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM