Update Cocoa -Binding - NSArrayController - ComboBox

in my application, I made a very simple binding. I have an NSMutableArray associated with an NSArrayController. The controller itself is connected to the ComboBox, and it displays the entire contents of NSMutableArray. It works great.

The problem is this: the contents of the array will change. If the user made some changes to the application, I delete all the elements in the NSMuteableArray and fill it with new and different elements.

But the binding of NSMutableArray ↔ NSArrayController ↔ NSComboBox is not updated.

Regardless of whether I delete all objects from the array, the ComboBox still shows the same elements.

What is wrong here? Is my approach wrong or do I only need to specify a binding in order to update myself? I did not know how to do this.

+3
source share
3 answers

You are probably editing the array behind the controller back, which undermines the KVO mechanism.

You said:

I have an NSMutableArray associated with an NSArrayController.

How? Where is the array located? In a document accessible through KVC / KVO-compatible -myArray / -setMyArray: accessory kit?

I bet you are directly telling "myArray" ivar about -removeAllObjects, right? How have these attributes changed KVC / KVO "knows" the array?

The answer is no. If you really replace the entire array, you want to tell your document (or the one who owns the array) the value of -setMyArray: to a whole new array. This will cause the correct KVO calls.

... , ? , :

( - . " " ) http://tinyurl.com/yb2zkr5

+6

( ARC/OS X 10.7):

, arrayInstance arrayController

 @property (weak) IBOutlet NSArrayController *arrayController;
 @property (strong) NSArray *arrayInstance; // for the array instance

 @synthesize arrayController = _arrayController;
 @synthesize arrayInstance = _arrayInstance;

 _arrayInstance = .......  // What ever the new array will be
 [_arrayController setContent:_arrayInstance];

arrayController .

, 2- :

 [self willChangeValueForKey:@"arrayInstance"];
 _arrayInstance = .......  // What ever the new array will be
 [self didChangeValueForKey:@"arrayInstance"];

, , - KVO-.

+1

KVC / KVO compliance is apparently a problem. You must create a new array and update the link with a new object using the created access methods. Otherwise, you can run KVO messages about the array being updated to inform the bindings that the contents of the array have changed.

Christian

0
source

Source: https://habr.com/ru/post/1721057/


All Articles