• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Limiting NSTextField to numbers?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Limiting NSTextField to numbers?


  • Subject: Re: Limiting NSTextField to numbers?
  • From: Peter Hudson <email@hidden>
  • Date: Wed, 21 Jan 2009 17:44:51 +0000

In IB in the Library panel, just below half way down there is a symbol on the left hand side which is a dollar sign ( and a calendar symbol next to it ) which is the number formatter. Click this and drag it to the text field - and drop it on it. Once the formatter is applied to the text field there is a small circle to the lower right of the edge of the text field - with a dollar in it. Click this symbol and the formatter setup panel will appear in the Attributes Inspector. Set away !

There is however a slight problem in using formatters for input. If you set it for numbers only, when the user types in an illegal character ( something other than a digit ) and tries to tab out of the field, they simply can't escape. Likewise if you set a target / action and they try to press return to complete the entry. There must be ways of frigging with the formatter to change this - but I've never bothered.

Instead of using a formatter, if I want to collect digits from the user I simply subclass the NSTextField and override - (void)textDidChange:(NSNotification *)aNotification. Then every time the user enters something you can check what they have written and either allow it or not.

For example to convert input to uppercase I do the following :-


-(void)textDidChange:(NSNotification *)aNotification { if( [upperCaseBtn state] == NSOnState ) { [self setStringValue:[[self stringValue] uppercaseString]]; } }



peter
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: NSURLConnection changing the URL
  • Next by Date: Re: Is there any mirroring in the Cocoa UI ?
  • Previous by thread: Re: Limiting NSTextField to numbers?
  • Next by thread: Re: Limiting NSTextField to numbers?
  • Index(es):
    • Date
    • Thread