可可绑定问题; 绑定表列不显示任何数据,控制台中没有错误
我在将 Cocoa 项目从手动同步接口模型转换为绑定模型时遇到问题,这样我就不必担心接口粘合代码。
我按照 CocoaDevCentral Cocoa Bindings 教程 来确保我已经涵盖了所有基础知识,但是事情无法正常工作。 我有一个主从界面,但我什至无法让界面的主部分正常工作。 尽管我已经设置了类似于教程中显示的绑定模型,但主列中没有显示任何数据。 我已确保所有控制器和对象都具有 -(id)key
和 -(void)setKey:(id)key
方法,以便它们符合绑定要求,我在笔尖中创建了一个 ControllerAlias 对象,将其连接到我的控制器,创建了一个 NSArrayController,该 NSArrayController 绑定到 ControllerAlias 连接到的类中的 NSMutableArray 之一,确保设置了数组中包含的对象的类型,然后我将一个表列绑定到 NSArrayController。
我在控制台中没有收到任何错误,并且将 NSBindingDebugLogLevel 设置为 1 也不会产生任何错误,这将帮助我找出问题所在。
我能想到的确保一切正常工作的唯一另一件事是检查连接到 NSArrayController 的 NSMutableArray 中是否确实有东西,而且确实如此。
有什么建议么? 我应该检查 Cocoa 绑定还有哪些其他典型陷阱?
I'm having trouble converting my Cocoa project from a manually-synched interface model to a bindings model so that I don't have to worry about interface glue code.
I followed the CocoaDevCentral Cocoa Bindings tutorial to make sure that I had covered all the bases, but things aren't working correctly. I have a master-detail interface, but I'm having trouble even getting the master portion of the interface to work correctly. No data is showing up in the master column, even though I've set up the bindings model similar to how it is shown in the tutorial. I've made sure all my controllers and objects have -(id)key
and -(void)setKey:(id)key
methods so that they're bindings-compliant, I've created a ControllerAlias object in my nib, connected it to my controller, created an NSArrayController that binds to one of the NSMutableArrays from the class that ControllerAlias connects to, made sure to set the type of objects that are contained within the array, and then I've bound a table column to the NSArrayController.
I'm getting no errors whatsoever in the Console, and setting NSBindingDebugLogLevel
to 1 doesn't produce any errors either, that would help me figure out what the problem is.
The only other thing I could think of to make sure that things are working correctly is to check that the NSMutableArray that connects to the NSArrayController actually has something in it, and it does.
Any suggestions? What other typical pitfalls are there with Cocoa bindings that I should check?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否在 key: 方法中放置了断点来确定它是否被调用? 如果不是,则表明表列中的绑定设置不正确(因为您已经验证数组中确实包含项目)。
我认为您不再需要创建对象控制器(该教程有点过时了)。 只需在您的 NIB 中创建一个对象,并将其类设置为您的控制器类。 您可以直接通过它而不是 ObjectController 设置绑定。
为了设置绑定,我执行以下操作:
这应该是您需要做的全部 - 我认为自从绑定在几个版本前首次引入以来,他们已经清理了很多。
Have you put a breakpoint in your key: method to determine if it is getting called or not? If it isn't, then that would indicate that something isn't set up correctly for the binding in the table column (since you have verified that your array does have items in it).
I don't think that you need to create an Object Controller anymore (that tutorial is a bit out of date). Just create an Object in your NIB, and set its class to your Controller class. You can set up the bindings directly through it instead of the ObjectController.
To set up a binding, I do the following:
That should be all that you need to do - I think they've cleaned this up quite a bit since bindings were first introduced a few versions ago.
什么是“控制器别名”? 这是模型、控制器还是视图?
什么意思?
类没有 NSMutableArray。
您绑定了数组控制器的什么属性?
您将它绑定到什么对象?
您将该对象绑定到哪个关键路径?
您绑定了表列的什么属性?
您将其绑定到数组控制器的哪个属性(键路径)?
What is a “controller alias”? Is this a model, controller, or view?
What do you mean?
Classes don't have
NSMutableArray
s.What property of the array controller did you bind?
What object did you bind it to?
What key path of that object did you bind it to?
What property of the table column did you bind?
Which property (key path) of the array controller did you bind it to?
因此,在我的原始代码中,我在 awakeFromNib 中修改数组(NSArrayController 所代表的数组),而不是在 init 中,因此更改不会反映在界面中,因为我没有通过键值观察来修改数组方法。
我将代码从 更改
为:
我认为另一个解决方案是在 -(id)init 方法而不是 -(void)awakeFromNib 方法中进行加载,但这需要更大的重构,所以我没有这样做。
我通过添加一个按钮来通过 NSArrayController 在数组列表中创建一个新事物来解决这个问题,当我单击该按钮时,一个新事物被添加到数组中,并且我现有的数组也神奇地出现了。
So in my original code, I was modifying the array (which the NSArrayController was representing) in awakeFromNib, not in init, so the changes weren't being reflected in the interface since I wasn't modifying the array via a key-value observing method.
I changed the code from
to:
I think the other solution is to do the loading in the -(id)init method instead of the -(void)awakeFromNib method, but that required a larger refactor, so I didn't do that.
I figured this out by adding a button to create a new thing in the array list via the NSArrayController, and when I clicked the button, a new thing was added to the array and my existing array magically showed up as well.