Re: NSTextField filters
Re: NSTextField filters
- Subject: Re: NSTextField filters
- From: Daniel Todd Currie <email@hidden>
- Date: Tue, 13 Jul 2004 14:37:24 -0700
Here's how I did it in a project of mine... Criticisms are welcome.
AOInputFormatter is a subclass of NSFormatter, which is instantiated in
Interface Builder and linked to the NSTextField's formatter outlet.
============ AOInputFormatter.m ============
#import "AOInputFormatter.h"
@implementation AOInputFormatter
- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string
errorDescription:(NSString **)error
{
*obj = string;
return(YES);
}
- (NSString *)stringForObjectValue:(id)anObject
{
if([anObject isKindOfClass:[NSString class]])
{
return(anObject);
}
else
{
return(nil);
}
}
- (BOOL)isPartialStringValid:(NSString *)partialString
newEditingString:(NSString **)newString errorDescription:(NSString
**)error
{
char testChar;
// allow only numbers, period, slash, minus, e, E
if([partialString length] > 1)
{
testChar = [partialString characterAtIndex:([partialString
length] - 1)];
if((int)testChar < 45 || ((int)testChar > 57 && (int)testChar
!= 69 && (int)testChar != 101))
{
NSBeep();
return(NO);
}
}
*newString = partialString;
return(YES);
}
@end
================================
-- DTC
On 2004 Jul 13, at 14:08, Shawn Erickson wrote:
On Jul 13, 2004, at 11:39 AM, Bret Kurth wrote:
How can I install a key filter on an NSTextField? I have fought with
using an NSFormatter object for several days now. It hasn't helped
me and it brings along a lot of undesirable behavior.
I merely wish to test if the key pressed is within a set of my
choosing and allow or disallow the key based on the result.
Have you reviewed the following?
<http://developer.apple.com/documentation/Cocoa/Conceptual/
DataFormatting/Tasks/CreatingACustomFormatter.html>
What issues are you having with NSFormatter?
Anyway I believe overriding
isPartialStringValid:newEditingString:errorDescription gets you want
you want. Additionally have getObjectValue:forString:errorDescription:
pass out the string passed in (assuming you are just dealing with
strings) and stringForObjectValue: just take the object in and return
it as the string (assuming again you are just working with strings).
I wonder if anyone has a simple regexp based formatter available?
-Shawn
_______________________________________________
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.
_______________________________________________
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.