Cocoa bindings problem; bound table columns don't show any data, no errors in console

时光怂恿深爱的人放手 提交于 2019-12-06 10:43:31

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:

  1. Create an instance of my controller in the NIB.
  2. Create an NSArrayController, bind it to an array in my controller.
  3. For each column in the table, bind the value to a member of an object in the array controller.

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.

I've created a ControllerAlias object in my nib,

What is a “controller alias”? Is this a model, controller, or view?

connected it to my controller,

What do you mean?

created an NSArrayController that binds to one of the NSMutableArrays from the class that ControllerAlias connects to,

Classes don't have NSMutableArrays.

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?

… and then I've bound a table column to the NSArrayController.

What property of the table column did you bind?

Which property (key path) of the array controller did you bind it to?

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

theArray = [[NSMutableArray alloc] init];
[theArray addObject:newThing];

to:

theArray = [[NSMutableArray alloc] init];
NSMutableArray *bindingsCompliantArray = [self mutableArrayValueForKey:@"things"];
[bindingsCompliantArray addObject:newThing];

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!