Re: Equivalent of UITextField's textField:shouldChangeCharactersInRange:replacementString for NSTextField
Re: Equivalent of UITextField's textField:shouldChangeCharactersInRange:replacementString for NSTextField
- Subject: Re: Equivalent of UITextField's textField:shouldChangeCharactersInRange:replacementString for NSTextField
- From: Aki Inoue <email@hidden>
- Date: Tue, 27 Sep 2011 14:58:42 -0700
Eric,
We really recommend looking at the approach I described earlier using NSFormatter subclass for input validation.
- (BOOL)isPartialStringValid:(NSString **)partialStringPtr proposedSelectedRange:(NSRangePointer)proposedSelRangePtr originalString:(NSString *)origString originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString **)error;
-textView:shouldChangeTextInRange:replacementString: is used by other AppKit services including undo/redo, input services, & formatter validation, etc.
It's possible for you to interfere with these system services if your override method was not implemented with aware of them. The interface is designed to allow overriding these system services, too.
By using NSFormatter, your method can focus to the validation logic itself since it's invoked after other services.
That said, if you still really need to go that route, you don't need have custom field editor.
All NSControl subclass hosting a field editor is a text view delegate.
You can simply override the delegate method with your custom NSTextField subclass.
For example,
- (BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementString:(NSString *)aString {
if ([super textView:aTextView shouldChangeTextInRange:aRange replacementString:aString]) {
id delegate = [self delegate];
if ([delegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementString:)]) return [delegate textView:aTextView shouldChangeTextInRange:aRange replacementString:aString];
return YES;
}
return NO;
}
should give you essentially what you want.
Aki
On 2011/09/27, at 14:08, Eric Wing wrote:
> On 9/21/11, Jens Alfke <email@hidden> wrote:
>>
>> On Sep 21, 2011, at 7:42 PM, Eric Wing wrote:
>>
>>> I have been using the delegate callback
>>> textField:shouldChangeCharactersInRange:replacementString for
>>> UITextField.
>>> I am trying to port code over to Mac using NSTextField. Is there
>>> something that provides similar functionality?
>>
>> Yes, but it’s a bit more complicated due to some rough edges in the AppKit
>> API that got cleaned up in iOS. The delegate method you’re looking for is in
>> NSTextView, which is the actual view used to manage editing in a focused
>> NSTextField. Basically, you should read about “field editors” in the AppKit
>> docs. You’ll want to give your text field a custom field editor and set
>> yourself as its delegate.
>>
>> —Jens
>
> Thanks for the info. That turned out to be a really long side quest.
> Had to get into NSCell's and went into areas of NSSecureTextField that
> I shouldn't have.
>
> Anyway, I have my field editor mostly working, but I have one follow
> up issue. I originally was using:
> - (void) controlTextDidBeginEditing:(NSNotification*)notification
> to get an event callback when the field begins editing.
>
> It seems that once I added my custom field editor, I no longer get
> this callback. Strangely, I still get the callback for:
> - (void) controlTextDidEndEditing:(NSNotification*)notification
>
> Is there something I can do to get that first callback back working
> again, or is there a substitute? (Currently my workaround is to keep a
> bool on my shouldChangeCharactersInRange: to know if I just started
> editing, but I would like to know if there is something more elegant.)
>
> Thanks,
> Eric
> --
> Beginning iPhone Games Development
> http://playcontrol.net/iphonegamebook/
> _______________________________________________
>
> 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
_______________________________________________
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