gpt4 book ai didi

objective-c - 是否有必要重写 bind :toObject:withKeyPath:options: in an NSView subclass to implement binding?

转载 作者:太空狗 更新时间:2023-10-30 03:45:25 25 4
gpt4 key购买 nike

我有一个 NSView 子类,它具有我想绑定(bind)的属性。我在子类中实现了以下内容:

我的 View .h:

@property (readwrite, retain) NSArray *representedObjects;

我的 View .m:

@synthesize representedObjects;

+(void)initialize
{
[self exposeBinding: @"representedObjects"];
}


-(void)bind:(NSString *)binding toObject:(id)observableController withKeyPath:(NSString *)keyPath options:(NSDictionary *)options
{
if ([binding isEqualToString:@"representedObjects"]) {
[observableController addObserver: self forKeyPath:@"arrangedObjects" options:NSKeyValueChangeNewKey context:nil];
} else {
[super bind: binding toObject:observableController withKeyPath:keyPath options: options];
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"arrangedObjects"]) {
[self setRepresentedObjects: [object arrangedObjects]];
}
}

然后我在 -[AppController awakeFromNib] 中创建到 arrayController 的绑定(bind):

[myView bind:@"representedObjects" toObject:arrayController withKeyPath:@"arrangedObjects" options: nil];

这是实现绑定(bind)的正确方法吗?它涉及大量样板代码,这让我觉得我做错了什么。

我认为 NSObject 会自动实现我在 -bind:toObject:withKeyPath:options: 中手动完成的操作,但事实并非如此。如果我注释掉我的 -bind:toObject:withKeyPath:options:,则永远不会调用 setRepresentedObjects 方法。

附加信息:我做了一些更多的调查,得出的结论是我原来的方法是正确的,你必须超越 -bind:toObject:withKeyPath:options:。这是来自 Cocoa Bindings Programming Topics: How Do Bindings Work? 的引述:

In its bind:toObject:withKeyPath:options: method an object must as a minimum do the following:

  • Determine which binding is being set
  • Record what object it is being bound to using what keypath and with what options
  • Register as an observer of the keypath of the object to which it is bound so that it receives notification of changes

The code sample in Listing 2 shows a partial implementation of Joystick’s bind:toObject:withKeyPath:options: method dealing with just the angle binding.

Listing 2 Partial implementation of the bind:toObject:withKeyPath:options method for the Joystick class:

static void *AngleBindingContext = (void *)@"JoystickAngle";

- (void)bind:(NSString *)binding
toObject:(id)observableObject
withKeyPath:(NSString *)keyPath
options:(NSDictionary *)options
{
// Observe the observableObject for changes -- note, pass binding identifier
// as the context, so you get that back in observeValueForKeyPath:...
// This way you can easily determine what needs to be updated.

if ([binding isEqualToString:@"angle"])
{
[observableObject addObserver:self
forKeyPath:keyPath
options:0
context:AngleBindingContext];

// Register what object and what keypath are
// associated with this binding
observedObjectForAngle = [observableObject retain];
observedKeyPathForAngle = [keyPath copy];

// Record the value transformer, if there is one
angleValueTransformer = nil;
NSString *vtName = [options objectForKey:@"NSValueTransformerName"];
if (vtName != nil)
{
angleValueTransformer = [NSValueTransformer
valueTransformerForName:vtName];
}
}
// Implementation continues...

这清楚地表明 Joystick 类(它是 NSView 的子类)需要覆盖 -bind:toObject:withKeyPath:options:

我觉得这很奇怪。我对这个结论持怀疑态度,因为我没有发现其他代码示例可以做到这一点。但是,正如 Apple 官方文档所说,我应该超越 -bind:toObject:withKeyPath:options: 我的结论是这是正确的方法。

如果有人能证明我是错的,我会很高兴!

最佳答案

不,你不应该需要那个胶水代码。

“似乎并非如此”是什么意思?如果省略它会怎样?

关于objective-c - 是否有必要重写 bind :toObject:withKeyPath:options: in an NSView subclass to implement binding?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/366938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com