Objective-C: correctly handling common properties between a subclass of NSControl and NSActionCell?

One of the things I've always doubted about is how to properly handle the relationship between a custom subclass NSControland a subclass NSCell. During my introduction to Cocoa, I saw him mention several times how a parent control provides many of the same methods, accessories, and mutators as implementing child cells / cells. For example, the class NSControland NSCellhave in their header files -isEnabledand -setEnabled::

NSControl.h

- (BOOL)isEnabled;
- (void)setEnabled:(BOOL)flag;

NSCell.h

- (BOOL)isEnabled;
- (void)setEnabled:(BOOL)flag;

I understand that the class NSControlprovides cover methods for most of the properties found in NSCell. I'm more interested in knowledge: how are they implemented? Or even better, how to implement your own properties of your subclasses? Obviously, only Apple engineers really know what is going on inside their frameworks, but I thought maybe someone could shed some light on the best way to imitate the Apple cover method in its purest form.

I am terribly versed in materials, so I will give an example of what I am saying. Let's say I subclassed NSControlas follows:

BSDToggleSwitch.h

#import "BSDToggleSwitchCell.h"

@interface BSDToggleSwitch : NSControl

@property (nonatomic, strong) BSDToggleSwitchCell *cell;
@property (nonatomic, assign) BOOL sharedProp;

@end

And I subclassed NSActionCell:

BSDToggleSwitchCell.h

#import "BSDToggleSwitch.h"

@interface BSDToggleSwitchCell : NSActionCell

@property (nonatomic, weak) BSDToggleSwitch *controlView;
@property (nonatomic, assign) BOOL sharedProp;

@end

As you can see, both of them have a property with a name sharedProp.

: , ? , , , , " ".

, , , , . ? , ? ? ?

, ( ):

  • Cocoa . ( ):

    [self bind:@"sharedProp" toObject:self.cell withKeyPath:@"sharedProp" options:nil];
    

    , /? KVO/KVC/Bindings, , . ?

  • . :

    - (void)setSharedProp:(BOOL)sharedProp
    {
        if ( sharedProp == _sharedProp )
            return;
    
        _sharedProp = sharedProp;
    
        [self.cell setSharedProp:sharedProp];
    }
    

    :

    - (void)setSharedProp:(BOOL)sharedProp
    {
        if ( sharedProp == _sharedProp )
            return;
    
        _sharedProp = sharedProp;
    
        [self.controlView setSharedProp:sharedProp];
    }
    

    , . , , ? , , .

  • . :

    static void * const BSDPropertySyncContext = @"BSDPropertySyncContext";
    
    - (void)observeValueForKeyPath:(NSString *)keyPath 
                          ofObject:(id)object 
                            change:(NSDictionary *)change 
                           context:(void *)context
    {
        if ( context == BSDPropertySyncContext && [keyPath isEqualToString:@"sharedProp"] ) {
    
            BOOL newValue = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
    
            if ( newValue != self.sharedProp ) {
                [self setSharedProp:newValue];
            }
    
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
    

    , , if . , " ", ( ), .

, , , . Cocoa/Objective-C , . , , !

!

+3
1

-, , NSCell - NeXT. NSCell , , -, (, NSMatrix, -, NSMatrix). NSTableView 10.7 NSCollectionViewItem . , NSCell.

, , . :

- (BOOL)sharedProp {
  return self.cell.sharedProp;
}

- (void)setSharedProp:(BOOL)sharedProp {
  [self.cell setSharedProp:sharedProp];
}

KVO , keyPathsForValuesAffectingValueForKey: ( ). , - :

- (NSSet *)keyPathsForValuesAffectingSharedProp {
   return [NSSet setWithObject:@"cell.sharedProp"];
}

sharedProp cell.sharedProp.

+3

All Articles