I have a custom NSNumberFormatter which does validation. If I bind the text 
field using NSValueBinding then when validation fails an alert panel will 
automatically appear. However it is the binding machinery that brings up the 
alert panel. Apple integrates bindings with user interface elements using a ton 
of private classes.

If you have a custom binding then the primary method at your disposal is 
bind:toObject:withKeyPath:options: which for a custom binding has no 
integration user interface elements. The binding is only one-way, model changes 
are pushed to the view but the view must manually push changes back to the 
model. So in addition to writing code to push view changes to the model you 
must also write code to present an error alert panel and discard a change.

--Richard Charles

> On May 6, 2019, at 7:49 AM, Keary Suska <cocoa-...@esoteritech.com> wrote:
> 
> IIRC, you get this behavior automatically when validating with a formatter, 
> so applying a custom NSNumberFormatter subclass might be more canonical and 
> less kludgey.
> 
> HTH,
> 
> Keary Suska
> Esoteritech, Inc.
> "Demystifying technology for your home or business"
> 
>> On May 6, 2019, at 5:33 AM, Richard Charles <rcharles...@gmail.com> wrote:
>> 
>> Thank you Quincey, Sean, Gary and Alex for your comments and suggestions.
>> 
>> To recap I have a NSTextField subclass bound to an array controller using a 
>> derived value binding. The user selects one or more objects in a graphic 
>> view. Object properties are shown in an inspector containing the text field.
>> 
>> A custom derived value binding is needed because object properties are 
>> floating point values with limited precision. Values that are close to equal 
>> are considered equal from the user's point of view.
>> 
>> When garbage is entered into a text field and the user presses the return 
>> key an alert panel is presented as a sheet attached to the window. The user 
>> can choose "Discard Change" or "OK”.
>> 
>> Because a custom binding is used code must be added to present the error and 
>> recover from the error. My text field subclass implements the 
>> NSErrorRecoveryAttempting informal protocol.
>> 
>> My original discard change code did not work.
>> 
>> - (void)attemptRecoveryFromError:(NSError *)error
>>                     optionIndex:(NSUInteger)recoveryOptionIndex
>>                        delegate:(id)delegate
>>              didRecoverSelector:(SEL)didRecoverSelector
>>                     contextInfo:(void *)contextInfo
>> {
>>    // Discard Change
>>    if (recoveryOptionIndex == 1) {
>>        [self abortEditing]; // does not work
>>    }
>> }
>> 
>> Here is a solution that works well.
>> 
>> - (void)attemptRecoveryFromError:(NSError *)error
>>                     optionIndex:(NSUInteger)recoveryOptionIndex
>>                        delegate:(id)delegate
>>              didRecoverSelector:(SEL)didRecoverSelector
>>                     contextInfo:(void *)contextInfo
>> {
>>    // Discard Change
>>    if (recoveryOptionIndex == 1) {
>>        // Revert display back to original value.
>>        [self abortEditing];
>>        [self setObjectValue:self.cachedObjectValue];
>>        [self.window makeFirstResponder:self];
>>    }
>> }
>> 
>> The abortEditing message is needed otherwise the binding will push the 
>> updated cached object value to the model.
>> 
>> --Richard Charles

_______________________________________________

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

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

Reply via email to