Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
281
86
Im trying to limit the the character length in a text field but i keep having problem with my code

AppDelegate.h
Code:
- (BOOL)textField:(NSTextField *)textField;
AppDelegate.m
Code:
#define MAX_LENGTH 20

- (BOOL)textField:(NSTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length >= MAX_LENGTH && range.length == 0) 
    {
    	return NO;
    }
    else
    {return YES;}
}
what am i doing wrong i'm getting an error on
Code:
if (textField.text.length >= MAX_LENGTH && range.length == 0)
Property 'text' not found on an object of type 'NSTextField *'
 
Last edited by a moderator:
Which part of the documentation says it has a text property? Also please use code tags (I've edited your post).


PHP:
if (textField.text.length >= MAX_LENGTH && range.length == 0)

i also took a screen shot of the code if my code doesn't work can u please provide me with a code that works.

all i want is to limit the amount of numbers/letters that i can enter into a text field.
i found lots of codes online but they ether don't work or they are for iOS i need something for mac
 

Attachments

  • Screen Shot 2013-10-25 at 8.56.10 pm.png
    Screen Shot 2013-10-25 at 8.56.10 pm.png
    33.1 KB · Views: 118
PHP:
if (textField.text.length >= MAX_LENGTH && range.length == 0)

i also took a screen shot of the code if my code doesn't work can u please provide me with a code that works.

all i want is to limit the amount of numbers/letters that i can enter into a text field.
i found lots of codes online but they ether don't work or they are for iOS i need something for mac

That's your code. That is not documentation. The documentation is here.
 
On an unrelated note, look up the const keyword. Using #define instead of const is like using goto label instead of functions.
 
PHP:
if (textField.text.length >= MAX_LENGTH && range.length == 0)

i also took a screen shot of the code if my code doesn't work can u please provide me with a code that works.

all i want is to limit the amount of numbers/letters that i can enter into a text field.
i found lots of codes online but they ether don't work or they are for iOS i need something for mac

thats because an NSTextField doesn't have a "text" property. it is a subclass of "NSControl" and it has a method for "stringValue". you will need to take that "stringValue" and manipulate it that way via some of NSStrings methods.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.