Re: NSTextField "Discard Change"
Re: NSTextField "Discard Change"
- Subject: Re: NSTextField "Discard Change"
- From: Richard Charles <email@hidden>
- Date: Mon, 06 May 2019 05:33:51 -0600
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 (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