• 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: NSTextField and NSNumberFormatter
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTextField and NSNumberFormatter


  • Subject: Re: NSTextField and NSNumberFormatter
  • From: "Louis C. Sacha" <email@hidden>
  • Date: Thu, 19 Feb 2004 00:32:51 -0800

Hello...

NSNumberFormatter (and the rest of Apple concrete NSFormatter subclasses) generally only validate the entered text when the user takes some action that will end editing.

You will need to make a subclass of NSNumberFormatter, and override the isPartialStringValid:newEditingString:errorDescription: method to check the entered text each time the user enters a key.


- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error
/* this validates the field's value as each key is entered into textfield */
{
/* validate the partial string */
NSDecimalNumber *check; /* used to hold temp value */ if ([self getObjectValue:&check forString:partialString errorDescription:error]) {return TRUE;}
else
{
NSBeep();
return FALSE;
}
}


Once you have made your custom subclass of NSNumberFormatter, load its header into InterfaceBuilder. You will need to drag an NSNumberFormatter from the pallete to the window that shows the objects in your nib (not over the NSTextField). Select your class in the "Custom Class" pane of the inspector for that NSNumberFormatter instance. You will need to make the "formatter" connection from your NSTextField to this instance of NSNumberFormatter.


In order to allow only positive numbers, you simply need to set a minimum value of 0 for the formatter, which you can do in one of the standard NSNumberFormatter inspector panes in InterfaceBuilder, after selecting the NSNumberFormatter that you dragged to your nib file.


Hope that helps,

Louis


Hello

I want NSTextField should only accept number and no other character.
For that I dropped NSNumberFormatter over NSTextField object in nib.
But while running I found that NSTextField accpet alphabet.

What is wrong ? Do I have to do something in code?

Can NSTextField be formatted to accpet only positive number?

Thanks in advance.

SBH
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >NSTextField and NSNumberFormatter (From: "Hire" <email@hidden>)

  • Prev by Date: NSTextField and NSNumberFormatter
  • Next by Date: Compiling trouble
  • Previous by thread: NSTextField and NSNumberFormatter
  • Next by thread: Compiling trouble
  • Index(es):
    • Date
    • Thread