开发者

Getting the length of an NSTextField's text property

开发者 https://www.devze.com 2023-03-06 09:44 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消