カスタム オブジェクトにバインドして、そのオブジェクトの特定のプロパティが変更されたときに通知を受け取ることはできますか?
例:
各営業担当者オブジェクトには、自動車とトラックの販売に関する統計を含む販売統計オブジェクトへの参照があります。カスタムNSView
は、車とトラックの両方の販売を 1 つのグラフィックに描画するために使用されます。
カスタム ビューは完全な統計オブジェクトにアクセスする必要があるため、それを にバインドしましたsalesRep.salesStats
。
ただし、carSales
プロパティを変更すると、ビューを更新する方法が必要です。親オブジェクトにバインドされているため、現在は更新されません。
販売タイプごとにバインディングを確立することは避けたいと思います (これは単なる例であり、私の特定のケースはより複雑です)。
@interface SBSalesRep : NSObject {
@property (strong) SBSalesStatistics *salesStats;
}
@interface SBSalesStatistics : NSObject
{
@property (assign) NSInteger carSales;
@property (assing) NSInteger truckSales;
}
@interface SBProgressView : NSView
{
@property (strong) SBSalesStatistics *statsToDisplay;
}
// Setup
SBSalesRep *salesRep = [SBSalesRep new];
// Bind to stats object, because we need car and tuck sales:
[progressView bind:@"statsToDisplay"
toObject:salesRep
withKeyPath:@"salesStats" // How to get notified of property changes here?
options:nil];
// This needs to trigger an update in the statistics view
salesRep.salesStats.carSales = 50;
// I tried this, but it does not work:
[salesRep willChangeValueForKey:@"salesStatistics"];
salesRep.salesStats.carSales = 50;
[salesRep didChangeValueForKey:@"salesStatistics"];