• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Custom canRemove binding
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Custom canRemove binding


  • Subject: Re: Custom canRemove binding
  • From: Scott Anguish <email@hidden>
  • Date: Wed, 5 May 2004 15:56:11 -0400

Yet another suggestion..


instead of adding another method, you could bind the enabled binding of the button to the canRemove binding, and bind enabled2 to theArrayController.content.count using a value transformer that returned true if the value was greater than 1 (allowing deletion). if they are both true, then the logical AND is true, and the button is enabled.

the value transformer is easily reused elsewhere (I'm sure I've got one stashed away that I wrote and have used a few times, but it's trivial to write) and you have no additional dependent keys.


+ (Class)transformedValueClass;
{
return [NSNumber class];
}

+ (BOOL)allowsReverseTransformation;
{
return NO;
}

- (id)transformedValue:(id)value;
{
float incomingNumber;
NSLog(@"value - %@",value);
if (value == nil) return nil;

// Attempt to get a reasonable value from the
// value object.
if ([value respondsToSelector: @selector(doubleValue)]) {
// handles NSString and NSNumber
incomingNumber = [value doubleValue];
} else {
[NSException raise: NSInternalInconsistencyException
format: @"Value (%@) does not respond to -doubleValue.",
[value class]];
}

return [NSNumber numberWithBool: (incomingNumber > 1)];
}

On May 5, 2004, at 10:54 AM, ian wrote:

On 5 May 2004, at 15:22, Allan Odgaard wrote:

On 5. May 2004, at 14:26, ian wrote:

I'd like to prevent the user from deleting the last row in the table however, so I bound the remove button to File's Owner (MyDocument) and a model key path of "myCanDelete" which is the name of a method that returns ([theArray count]>1)

Trouble is, this is called once only at startup, so the button doesn't update.

And here's the third suggestion! :)

Given that 'contentArray' of your array controller is bound to [File's Owner].myArray then add this to MyDocument.m (see NSKeyValueObserving protocol description for details):

+ (void)initialize
{
[self setKeys:[NSArray arrayWithObject:@"myArray"] triggerChangeNotificationsForDependentKey:@"myCanDelete"];
}


Crikey, that's smooth, very elegant. Now the whole validation thing happens automagically without explicitly informing observers that something interesting happened.

Cool :)
Ian
_______________________________________________
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.


  • Follow-Ups:
    • Array operators (was: Custom canRemove binding)
      • From: Allan Odgaard <email@hidden>
References: 
 >Custom canRemove binding (From: ian <email@hidden>)
 >Re: Custom canRemove binding (From: Allan Odgaard <email@hidden>)
 >Re: Custom canRemove binding (From: ian <email@hidden>)

  • Prev by Date: Re: Cut/Copy/Paste in NSTextField controls
  • Next by Date: Re: where to find more applicable info on cocoa development
  • Previous by thread: Re: Custom canRemove binding
  • Next by thread: Array operators (was: Custom canRemove binding)
  • Index(es):
    • Date
    • Thread