It's in NSFormatter. You just override it in your subclass.

Cheers,

Andre Masse

------------------------------------------------------------------------

        William Squires <mailto:wsqui...@satx.rr.com>
September 28, 2011 20:48


Okay, but where does the code go? Is this a delegate method in NSTextField, or in it's parent window's view controller?



------------------------------------------------------------------------

        Andre Masse <mailto:andre.ma...@videotron.ca>
September 28, 2011 17:04


You can use formatters for this too. Here's the code I'm using (modify the characters you need to allow in addCharactersInString:).


// allow only digits and +-*/.
- (BOOL)isPartialStringValid:(NSString **)partialStringPtr
       proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
              originalString:(NSString *)origString
       originalSelectedRange:(NSRange)origSelRange
            errorDescription:(NSString **)error
{
NSMutableCharacterSet *alphaNums = [NSMutableCharacterSet decimalDigitCharacterSet];
    [alphaNums addCharactersInString:@"+-*/."];
NSCharacterSet *inStringSet = [NSCharacterSet characterSetWithCharactersInString:*partialStringPtr];

    if ([alphaNums isSupersetOfSet:inStringSet]) {
        return YES;
    }
    return NO;
}

Cheers,

Andre Masse
------------------------------------------------------------------------

        Graham Cox <mailto:graham....@bigpond.com>
September 28, 2011 03:02




The preferred, supported way is to use a NSFormatter attached to the field. This has the ability to fully constrain and convert values to and from the field's text representation. This is designed to be subclassed. It's unusual to subclass NSTextField itself, especially as it's not the text field that is actually performing the text entry, but a hidden NSTextView called the field editor. Formatters can disallow the focus to be changed away from the text field if the content is not valid. To prevent characters from being entered as they're typed, you'll also want to use the field editor's delegate methods to reject unwanted characters (formatters only validate after the entry is submitted).

--Graham


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/andre.masse%40videotron.ca

This email sent to andre.ma...@videotron.ca

------------------------------------------------------------------------

        William Squires <mailto:wsqui...@satx.rr.com>
September 27, 2011 21:41


Hi,
I have a need for a complex data-entry form (window). Some fields are supposed to only accept alpha characters (upper or lower), some are supposed to only take integers, others to only take floats (but no negative values allowed). Is there a general way of validating the contents of a field before it resigns first responder status? Even better, is there a way to encapsulate that behavior in an NSTextField subclass so that I can make, say, an AlphaOnlyTextField class, drag a (normal) NSTextField onto the window in IB, then change it's super to "AlphaOnlyTextField", or some such? This way I can save a lot of time, and will have a reusable control (sub)class that I can then use in other projects!
I'm still using Xcode 3.somethingorother, and IB for my MacOS X/iOS work.
Here's the control flow I hope to tap into:

Control (an NSTextField) has focus (is first responder) - user types something into the NSTextField User tabs (or shift-tabs) to the next control, or clicks the mouse on another control NSTextField that currently has first responder status checks to see if it has a delegate, and - if it does - does it respond to validate:? If so, the delegate gets sent the validate: message which returns a BOOL. If the control's contents (it's stringValue in the case of an NSTextField) has valid input, it'll return YES to indicate that the control may lose first responder. If it returns NO, then the attempt to lose first responder status is nullified, and the control keeps its first responder status. User finally types in valid input, and the validate: returns YES. The NSTextField now loses first responder, and some other control on the view now gets first responder status.

Doable? Basically, I'd like to subclass NSTextField and the subclass would implement the delegate protocol's validate: message (method); the subclass would set itself as it's own delegate in the awakeFromNib: method, then remove itself somewhere else (when the control, and it's parent window, dies.) Even better, can I trap (and kill) invalid characters as they're entered? (i.e. an NSTextField that only allows (A-Z | a-z), and converts the characters to upper- or lower-case, say?


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/andre.masse%40videotron.ca

This email sent to andre.ma...@videotron.ca

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to