Re: Limiting NSTextField to numbers?
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