Limit textfield character length

Discussion in 'Mac Programming' started by abcdefg12345, Oct 24, 2013.

  1. abcdefg12345, Oct 24, 2013
    Last edited by a moderator: Oct 25, 2013

    abcdefg12345 macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    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 *'
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Which part of the documentation says it has a text property? Also please use code tags (I've edited your post).
     
  3. abcdefg12345 thread starter macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #3

    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
     

    Attached Files:

  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    That's your code. That is not documentation. The documentation is here.
     
  5. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    On an unrelated note, look up the const keyword. Using #define instead of const is like using goto label instead of functions.
     
  6. devilofspades macrumors member

    Joined:
    Jul 20, 2011
    #6
    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.
     

Share This Page