myVIEw.h:
@property (reaDWrite,retain) NSArray *representedobjects;
myVIEw.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的绑定:
[myVIEw bind:@"representedobjects" toObject:arrayController withKeyPath:@"arrangedobjects" options: nil];
这是实现绑定的正确方法吗?它涉及很多锅炉板代码,这让我觉得我做错了.
我认为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 changesThe 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:
06003
这清楚地表明,Joystick类(它是一个NSVIEw子类)需要覆盖-bind:toObject:withKeyPath:options:.
我觉得这很令人惊讶.我对此结论持怀疑态度,因为我没有找到其他代码示例.但是,正如苹果公司的官方文档所说,我应该克服-bind:toObject:withKeyPath:options:我得出结论认为这是正确的方法.
如果有人能证明我错了,我会很高兴的!
你的意思是“似乎并非如此”?如果省略它会怎么样?
总结以上是内存溢出为你收集整理的objective-c – 是否有必要覆盖bind:toObject:withKeyPath:options:在NSView子类中实现绑定?全部内容,希望文章能够帮你解决objective-c – 是否有必要覆盖bind:toObject:withKeyPath:options:在NSView子类中实现绑定?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)