I'm trying to add a bindable property to a custom NSPopUpButton subclass.
I've created a "selectedKey" property, which is meant to store a NSString associated with selected menu item.
In control init, I set self as button target and an action for the button (valueChanged:), which in turn sets "selectedKey" in accordance with user selection:
#interface MyPopUpButton : NSPopUpButton {
NSMutableDictionary *_items;
NSString *_selectedKey;
}
#property(nonatomic, readwrite, copy) NSString* selectedKey;
- (void)addItemWithTitle:(NSString *)title andKey:(NSString *)key;
#end
#implementation MyPopUpButton
- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
_items = [NSMutableDictionary new];
[NSObject exposeBinding:#"selectedKey"];
[super setTarget:self];
[super setAction:#selector(valueChanged:)];
}
return self;
}
- (void)addItemWithTitle:(NSString *)title andKey:(NSString *)key {
[super addItemWithTitle:title];
[_items setValue:title forKey:key];
}
- (void)valueChanged:(id)sender {
for (NSString *aKey in [_items allKeys]) {
if ([[_items valueForKey:aKey] isEqualToString:[self titleOfSelectedItem]]) {
self.selectedKey = aKey;
}
}
}
- (void)setSelectedKey:(NSString *)selectedKey {
[self willChangeValueForKey:#"selectedKey"];
_selectedKey = selectedKey;
[self didChangeValueForKey:#"selectedKey"];
[self selectItemWithTitle:[_items valueForKey:selectedKey]];
}
#end
This seems to work as expected: "selectedKey" property is changed when user changes PopUpButton selection.
Unfortunately, trying to bind this property, doesn't work.
[selectButton bind:#"selectedKey" toObject:savingDictionary withKeyPath:key options:#{NSContinuouslyUpdatesValueBindingOption : #YES }]
When selection is changed bind object is not updated accordingly.
What am I doing wrong?
I've created a "selectedKey" property, which is meant to store an NSString associated with selected menu item.
Bindings is definitely the way to go here, but your use of bind:toObject:withKeyPath:options is incorrect.
The value that you pass to the first argument must be one of the predefined values made available by Apple for that particular control. For NSPopUpButton objects, the available values are documented in the NSPopUpButton Bindings Reference. When you look through this document you'll see that there is no selectedKey option. There is however a selectedValue which has the following description:
An NSString that specifies the title of the selected item in the NSPopUpButton.
Thus the correct way to set up the binding is as follows:
[self.btn bind:#"selectedValue"
toObject:self
withKeyPath:#"mySelectedString"
options:nil];
This is all you need to do: when the action selector is fired the property stored at the keyPath you passed in as the third argument will already have been updated. This means that you can (i) get rid of the setSelectedKey method entirely, (ii) remove exposeBinding line, and (iii) remove the code within valueChanged: - Cocoa has already done this bit.
The example below implements just two methods, but, if I've understood your intentions, they should be all you need:
- (void)awakeFromNib {
self.btn.target = self;
self.btn.action = #selector(popUpActivity:);
[self.btn bind:#"selectedValue"
toObject:self
withKeyPath:#"mySelectedString"
options:nil];
// I've added a couple of additional bindings here; they're
// not required, but I thought they'd be instructive.
[self.btn bind:#"content"
toObject:self
withKeyPath:#"myItems"
options:nil];
[self.btn bind:#"selectedIndex"
toObject:self
withKeyPath:#"mySelectedIndex"
options:nil];
// Now that you've set the bindings up, use them!
self.myItems = #[#"Snow", #"Falling", #"On", #"Cedars"];
self.mySelectedIndex = #3; // "Cedars" will be selected on startup
// no need to set value of mySelectedString, because it will be
// updated automatically by the selectedIndex binding.
NSLog("%#", self.mySelectedString) // -> "Cedars"
}
- (void)popUpActivity:(id)sender {
NSLog(#"value of <selectedIndex> -> %#", self.mySelectedIndex);
NSLog(#"value of <selectedString> -> %#", self.mySelectedString);
}
A final point worth making is that none of the above should be a part of an NSPopUpButton subclass. It looks like you can - and therefore should - do everything you need to do without a custom subclass of this control. In my demo-app the code above belongs to the ViewController class, you should try doing this also.
Related
I have a NSTableView populated by a Core Data entity and Add Item / Remove Item buttons all wired with a NSArrayController and bindings in Interface Builder.
The Undo/Redo menu items can undo or redo the add / remove item actions.
But the menu entries are called only „Undo“ resp. „Redo“.
How can i name them like „Undo Add Item“, „Undo Remove Item“, etc.
(I am aware, something similar was asked before, but the accepted answers are either a single, now rotten link or the advice to subclass NSManagedObject and override a method that Apples documentation says about: "Important: You must not override this method.“)
Add a subclass of NSArrayController as a file in your project. In the xib, in the Identity Inspector of the array controller, change the Class from NSArrayController to your new subclass.
Override the - newObject method.
- (id)newObject
{
id newObj = [super newObject];
NSUndoManager *undoManager = [[[NSApp delegate] window] undoManager];
[undoManager setActionName:#"Add Item"];
return newObj;
}
Also the - remove:sender method.
- (void)remove:(id)sender
{
[super remove:sender];
NSUndoManager *undoManager = [[[NSApp delegate] window] undoManager];
[undoManager setActionName:#"Remove Item"];
}
Register for NSManagedObjectContextObjectsDidChangeNotification:
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(mocDidChangeNotification:)
name:NSManagedObjectContextObjectsDidChangeNotification
object: nil];
And parse the userInfo dictionary in the corresponding method:
- (void)mocDidChangeNotification:(NSNotification *)notification
{
NSManagedObjectContext* savedContext = [notification object];
// Ignore change notifications for anything but the mainQueue MOC
if (savedContext != self.managedObjectContext) {
return;
}
// Ignore updates -- lots of noise from maintaining user-irrelevant data
// Set actionName for insertion
for (NSManagedObject* insertedObject in
[notification.userInfo valueForKeyPath:NSInsertedObjectsKey])
{
NSString* objectClass = NSStringFromClass([insertedObject class]);
savedContext.undoManager.actionName = savedContext.undoManager.isUndoing ?
[NSString stringWithFormat:#"Delete %#", objectClass] :
[NSString stringWithFormat:#"Insert %#", objectClass];
}
// Set actionName for deletion
for (NSManagedObject* deletedObject in
[notification.userInfo valueForKeyPath:NSDeletedObjectsKey])
{
NSString* objectClass = NSStringFromClass([deletedObject class]);
savedContext.undoManager.actionName = savedContext.undoManager.isUndoing ?
[NSString stringWithFormat:#"Insert %#", objectClass] :
[NSString stringWithFormat:#"Delete %#", objectClass];
}
}
I've tested this in my own code-- it's rough. Can spend a lot more time making the actionName nicer. I deleted parsing of updates because: 1) insertions and deletions of objects in to-many relationships generate updates of other objects 2) I don't care to figure out how to discover what properties changed at this time
I also have class names that aren't user-friendly, so this is a great time to implement the description function for all entities, and use that rather than the class name.
But this at least works for all object controllers in a project, and easily enough for insert and delete.
[edit] Updated with mikeD's suggestion to cover redo having an inverse name. Thanks!
AAA.m:
- (void)keepCurrentArray:(id)object
{
_currentTest=[[NSMutableArray alloc]init];
[_currentTest addObject:#"one"];
[_currentTest addObject:#"two"];
[_currentTest addObject:object];
NSLog(#"My Array is:%#",_currentTest);
}
Class BBB.m is passing objects to class AAA.
Right now if i'm passing X to the above method so the array will be: one,two,X . Then i'll send it Y and the array will be one,two,Y instead of what i want to accomplish: one,two,x,one,two,y.
Is that because I'm alloc and init _currentTest every time? How can I solve it?
Update:
I had a few suggestions on how to solve this and none of them worked for me. I've created a new project with just the code in the answers and i'm still getting the same result when I try to add the second object i get: one, two, test instead of one,two,test,one,two,test
Yes, it's because that you're alloc and init-ing every time you run that method. Instead, put _currentTest = [[NSMutableArray alloc] init]; in AAA.m's init method.
AAA.m
-(id)init
{
if ((self = [super init]))
_currentTest = [[NSMutableArray alloc] init];
return self;
}
- (void)keepCurrentArray:(id)object
{
[_currentTest addObject:#"one"];
[_currentTest addObject:#"two"];
[_currentTest addObject:object];
NSLog(#"My Array is:%#",_currentTest);
}
_currentTest=[[NSMutableArray alloc]init]; in a method is never a good thing!!!
As per naming convention it seems to be a property to the AAA Class. So for property, the alloc+init should be either in init or awakeFromNib. So that if is initialized just once.
However in some situations init is called more than once then your previous values are lost and new set are added.
So what you can do is make another class and put this _currentTest Array there and make it static and use it here. I hope this will work fine. And make sure in the init method of that class it is initialized just once, as :
//**this is not compiled and checked may contains typo and errors**
#implementation Storage
static NSMutableArray *yourStaticArray;
-(id)init{
self = [super init];
if (self) {
if (!yourStaticArray) {
yourStaticArray=[NSMutableArray new];
}
}
return self;
}
-(void)addYourStaticArray:(NSString *)val{
[yourStaticArray addObject:val];
}
-(NSArray *)yourStaticArray {
return yourStaticArray ;
}
#end
Well you need to have a property for that _currentTest if you want to be able to keep it around between method call.
Put this in your .h file
#property (nonatomic, copy) NSMutableArray * currentTest;
And this in hour .m file
- (NSMutableArray *)currentTest
{
if (!_currentTest)
_currentTest = [[NSMutableArray alloc] initWithCapacity:11];
return _currentTest;
}
- (void)keepCurrentArray:(id)object
{
[self.currentTest addObject:#"one"];
[self.currentTest addObject:#"two"];
[self.currentTest addObject:object];
NSLog(#"My Array is:%#", self.currentTest);
}
I Just try the code you've put on drop box and it's working exactly as it is suppose to, the array keeps it's value and everything,
BUT
Exactly as it is suppose to is not what you are trying to achieve
Your problem is not in AAA.m, your problem is in BBB.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *kios = [ViewController new];
[kios keepCurrentArray:#"Test"];
[kios keepCurrentArray:#"Test2"];
}
I took the liberty of adding the #"test2" to the code you've send. If you run it you will see that your array still exist when the second call is made.
The REAL problem here is that you are creating a NEW ViewController each time. A brand new one, it is normal that it is empty (clean), it's a new one.
If I buy a note pad monday and fill it up, I don't expect when I'm buying an other one on friday to be already fill with the stuff I've wrote on monday in the previous one.
But this is exactly that behaviour that you are expecting from your ViewController.
You need to store your NSMutableArray in an other object that doesn't
get destroy and created over and over again.
This is happening because you are creating a new array every time that your method is called. Basically, you need to see if it has already been created, and only create it if needed. You can change your method to:
- (void)keepCurrentArray:(id)object
{
if (!_currentTest)
{
_currentTest=[[NSMutableArray alloc]init];
}
[_currentTest addObject:#"one"];
[_currentTest addObject:#"two"];
[_currentTest addObject:object];
NSLog(#"My Array is:%#",_currentTest);
}
EDIT:
In addition to the above problem, you also have this code which needs to be corrected (comments removed):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *kios = [ViewController new];
[kios keepCurrentArray:#"Test"];
}
This code creates a new instance of ViewController every time that you click on a row in the table. Because you are creating a new instance instead of reusing the old one, you start with an empty array each time. In order to keep adding to the same array, you need to keep using the same view controller.
In order to do this, you need to add a declared property to your .h file, similar to your currentTest declared property:
#property (strong,nonatomic) ViewController *kios;
Then, change your action so that you only create a new view controller if needed (the first time) and then reuses it after that:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!_kios)
{
_kios = [ViewController new];
}
[_kios keepCurrentArray:#"Test"];
}
I'm having trouble getting KVO working with text fields that are bound together in a Cocoa app. I have gotten this to work when setting strings in NSTextFields with buttons but it is not working with bindings. As always, any help from Stack Overflow would be greatly appreciated.
Purpose of my code is to:
bind several text fields together
when a number is input in one field, have the other fields automatically update
observe the changes in the text fields
Here's my code for MainClass which is an NSObject subclass:
#import "MainClass.h"
#interface MainClass ()
#property (weak) IBOutlet NSTextField *fieldA;
#property (weak) IBOutlet NSTextField *fieldB;
#property (weak) IBOutlet NSTextField *fieldC;
#property double numA, numB, numC;
#end
#implementation MainClass
static int MainClassKVOContext = 0;
- (void)awakeFromNib {
[self.fieldA addObserver:self forKeyPath:#"numA" options:0 context:&MainClassKVOContext];
[self.fieldB addObserver:self forKeyPath:#"numB" options:0 context:&MainClassKVOContext];
[self.fieldC addObserver:self forKeyPath:#"numC" options:0 context:&MainClassKVOContext];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context != &MainClassKVOContext) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
return;
}
if (object == self.fieldA) {
if ([keyPath isEqualToString:#"numA"]) {
NSLog(#"fieldA length = %ld", [_fieldA.stringValue length]);
}
}
if (object == self.fieldB) {
if ([keyPath isEqualToString:#"numB"]) {
NSLog(#"fieldB length = %ld", [_fieldB.stringValue length]);
}
}
if (object == self.fieldC) {
if ([keyPath isEqualToString:#"numC"]) {
NSLog(#"fieldC length = %ld", [_fieldC.stringValue length]);
}
}
}
+ (NSSet *)keyPathsForValuesAffectingNumB {
return [NSSet setWithObject:#"numA"];
}
+ (NSSet *)keyPathsForValuesAffectingNumC {
return [NSSet setWithObject:#"numA"];
}
- (void)setNumB:(double)theNumB {
[self setNumA:theNumB * 1000];
}
- (double)numB {
return [self numA] / 1000;
}
- (void)setNumC:(double)theNumC {
[self setNumA:theNumC * 1000000];
}
- (double)numC {
return [self numA] / 1000000;
}
- (void)setNilValueForKey:(NSString*)key {
if ([key isEqualToString:#"numA"]) return [self setNumA: 0];
if ([key isEqualToString:#"numB"]) return [self setNumB: 0];
if ([key isEqualToString:#"numC"]) return [self setNumC: 0];
[super setNilValueForKey:key];
}
#end
And here is the binding for one of the text fields:
Key-Value Observing on NSTextFields
In your -awakeFromNib method's implementation, you've written
[self.fieldA addObserver:self
forKeyPath:#"numA"
options:0
context:&MainClassKVOContext];
This doesn't do what you're hoping it will: self.fieldA is not key-value coding compliant for the key numA: if you try sending -valueForKey: or -setValue:forKey: with the key #"numA" to self.fieldA, you'll get the following exceptions:
[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key numA.
and
[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key numA.
As a result, the NSTextField instances are not key-value observing compliant for #"numA", either: the first requirement to be KVO-compliant for some key is to be KVC-compliant for that key.
It is, however, KVO-compliant for, among other things, stringValue. This allows you to do what I described earlier.
Note: None of this is altered by the way that you've set up bindings in Interface Builder. More on that later.
The Trouble With Key-Value Observing on NSTextField's stringValue
Observing an NSTextField's value for #"stringValue" works when -setStringValue: gets called on the NSTextField. This is a result of the internals of KVO.
A Brief Trip Into KVO Internals
When you begin observing an key-value observing an object for the first time, the object's class is changed--its isa pointer is changed. You can see this happening by overriding -addObserver:forKeyPath:options:context:
- (void)addObserver:(NSObject *)observer
forKeyPath:(NSString *)keyPath
options:(NSKeyValueObservingOptions)options
context:(void *)context
{
NSLog(#"%p, %#", self->isa, NSStringFromClass(self->isa));
[super addObserver:observer
forKeyPath:keyPath
options:options
context:context];
NSLog(#"%p, %#", self->isa, NSStringFromClass(self->isa));
}
In general, the name of the class changes from Object to NSKVONotifying_Object.
If we had called -addObserver:forKeyPath:options:context: on an instance of Object with with the key path #"property"--a key for which instances of Object are KVC-compliant--when next we call -setProperty: on our instance of Object (in fact, now an instance of NSKVONotifying_Object), the following messages will be sent to the object
-willChangeValueForKey: passing #"property"
-setProperty: passing #"property"
-didChangeValueForKey: passing #"property"
Breaking within any of these methods reveal that they're called from the undocumented function _NSSetObjectValueAndNotify.
The relevance of all of this is that the method -observeValueForKeyPath:ofObject:change:context: is called on the observer that we added to our instance of Object for the key path #"property" from -didChangeValueForKey:. Here's the top of the stack trace:
-[Observer observeValueForKeyPath:ofObject:change:context:]
NSKeyValueNotifyObserver ()
NSKeyValueDidChange ()
-[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] ()
How does this relate to NSTextField and #"stringValue"?
In your previous setup, you were adding an observer to your text field on -awakeFromNib. This meant that your text field was already an instance of NSKVONotifying_NSTextField.
You would then press one or another button which in turn would call -setStringValue on your text field. You were able to observe this change because--as an instance of NSKVONotifying_NSTextField--your text field, upon receiving setStringValue:value actually received
willChangeValueForKey:#"stringValue"
setStringValue:value
didChangeValueForKey:#"stringValue"
As above, from within didChangeValueForKey:#"stringValue", all the objects which are observing the text field's value for #"stringValue" are notified that the value for this key has changed in their own implementations of -observeValueForKeyPath:ofObject:change:context:. In particular, this is true for the the object which you added as an observer for the text field in -awakeFromNib.
In summary, you were able to observe the change in the text field's value for #"stringValue" because you added yourself as an observer of the text field for that key and because -setStringValue was called on the text field.
So What's The Problem?
So far under the guise of discussing "The Trouble With Key-Value Observing on NSTextFields" we've only actually made sense of the opening sentence
Observing an NSTextField's value for #"stringValue" works when -setStringValue: gets called on the NSTextField.
And that sounds great! So what's the problem?
The problem is that -setStringValue: does not get called on the text field as the user is typing into it OR even after the user has ended editing (by tabbing out of the text field, for example). (Furthermore, -willChangeValueForKey: and -didChangeValueForKey: are not called manually. If they were, our KVO would work; but it doesn't.) This means that while our KVO on #"stringValue" works when -setStringValue: is called on the text field, it does NOT work when the user herself enters text.
TL;DR: KVO on the #"stringValue" of an NSTextField isn't good enough since it doesn't work for user input.
Binding An NSTextField's Value To A String
Let's try using bindings.
Initial Setup
Create an example project with a separate window controller (I've used the creative name WindowController) complete with XIB. (Here's the project I'm starting from on GitHub.) In WindowController.m added a property stringA in a class extension:
#interface WindowController ()
#property (nonatomic) NSString *stringA;
#end
In Interface Builder, create a text field and open the Bindings Inspector:
Under the "Value" header, expand the "Value" item:
The pop-up button next to the "Bind to" checkbox presently has "Shared User Defaults Controller" selected. We want to bind the text field's value to our WindowController instance., so select "File's Owner" instead. When this happens, the "Controller Key" field will be emptied and the "Model Key Path" field will be changed to "self".
We want to bind this text field's value to our WindowController instance's property stringA so change the "Model Key Path" to self.stringA:
At this point, we are done. (Progress so far on GitHub.) We have successfully bound the text field's value to our WindowController's property stringA.
Testing It Out
If we set stringA to some value in -init, that value will show up in the text field when the window loads:
- (id)init
{
self = [super initWithWindowNibName:#"WindowController"];
if (self) {
self.stringA = #"hello world";
}
return self;
}
And already, we have set up bindings in the other direction as well; upon ending editing in the text field, the our window controller's property stringA is set. We can check this by overriding it's setter:
- (void)setStringA:(NSString *)stringA
{
NSLog(#"%s: stringA: <<%#>> => <<%#>>", __PRETTY_FUNCTION__, _stringA, stringA);
_stringA = stringA;
}
Reply Hazy, Try Again
After typing some text into the text field and pressing tab, we'll see printed out
-[WindowController setStringA:]: stringA: <<(null)>> => <<some text>>
This looks great. Why haven't we been talking about this all along??? There's a bit of a hitch here: the pesky pressing tab thing. Binding a text field's value to a string does not set the string value until editing has ended in the text field.
A New Hope
However, there is still hope! The Cocoa Binding Documentation for NSTextField states that one binding option available for an NSTextField is NSContinuouslyUpdatesValueBindingOption. And lo and behold, there is a checkbox corresponding to this very option in the Bindings Inspector for NSTextField's value. Go ahead and check that box.
With this change in place, as we type things in, the update to the window controller's stringA property is continuously logged out:
-[WindowController setStringA:]: stringA: <<(null)>> => <<t>>
-[WindowController setStringA:]: stringA: <<t>> => <<th>>
-[WindowController setStringA:]: stringA: <<th>> => <<thi>>
-[WindowController setStringA:]: stringA: <<thi>> => <<thin>>
-[WindowController setStringA:]: stringA: <<thin>> => <<thing>>
-[WindowController setStringA:]: stringA: <<thing>> => <<things>>
-[WindowController setStringA:]: stringA: <<things>> => <<things >>
-[WindowController setStringA:]: stringA: <<things >> => <<things i>>
-[WindowController setStringA:]: stringA: <<things i>> => <<things in>>
Finally, we're continuously updating the window controller's string from the text field. The rest is easy. As a quick proof of concept, add a couple more text fields to the window, bind them to stringA and set them to update continuously. You at this point have three synchronized NSTextFields! Here's the project with three synchronized text fields.
The Rest of the Way
You're wanting to setup three textfields that display numbers that have some relationship to each other. Since we're dealing with numbers now, we'll remove the property stringA from WindowController and replace it with numberA, numberB and numberC:
#interface WindowController ()
#property (nonatomic) NSNumber *numberA;
#property (nonatomic) NSNumber *numberB;
#property (nonatomic) NSNumber *numberC;
#end
Next we'll bind the first text field to numberA on File's Owner, the second to numberB, and so on. Finally we just need to add a property which is the quantity which is being represented in these different ways. Let's call that value quantity.
#interface WindowController ()
#property (nonatomic) NSNumber *quantity;
#property (nonatomic) NSNumber *numberA;
#property (nonatomic) NSNumber *numberB;
#property (nonatomic) NSNumber *numberC;
#end
We'll need the constant conversion factors to transform from the units of quantity to the units of numberA and so forth, so add
static float convertToA = 1000.0f;
static float convertToB = 573.0f;
static float convertToC = 720.0f;
(Of course, use the numbers that are relevant to your situation.) With this much, we can implement the accessors for each of the numbers:
- (NSNumber *)numberA
{
return [NSNumber numberWithFloat:self.quantity.floatValue * convertToA];
}
- (void)setNumberA:(NSNumber *)numberA
{
self.quantity = [NSNumber numberWithFloat:numberA.floatValue * 1.0f/convertToA];
}
- (NSNumber *)numberB
{
return [NSNumber numberWithFloat:self.quantity.floatValue * convertToB];
}
- (void)setNumberB:(NSNumber *)numberB
{
self.quantity = [NSNumber numberWithFloat:numberB.floatValue * 1.0f/convertToB];
}
- (NSNumber *)numberC
{
return [NSNumber numberWithFloat:self.quantity.floatValue * convertToC];
}
- (void)setNumberC:(NSNumber *)numberC
{
self.quantity = [NSNumber numberWithFloat:numberC.floatValue * 1.0f/convertToC];
}
All of the different number accessors are now just indirect mechanisms for accessing quantity, and are perfect for bindings. There is only one additional thing that remains to be done: we need to make sure that observers repoll all of the numbers whenever quantity is changed:
+ (NSSet *)keyPathsForValuesAffectingNumberA
{
return [NSSet setWithObject:#"quantity"];
}
+ (NSSet *)keyPathsForValuesAffectingNumberB
{
return [NSSet setWithObject:#"quantity"];
}
+ (NSSet *)keyPathsForValuesAffectingNumberC
{
return [NSSet setWithObject:#"quantity"];
}
Now, whenever you type into one of the textfields, the others are updated accordingly. Here's the final version of the project on GitHub.
I have a window with an NSTextField (in Snow Leopard), which I have binded to an NSString function in my WindowController class. This string will combine information about my table view's selection and count, provided by my array controller. It gets an initial value, "0 0", but doesn't ever update, when the selection or count changes. The binding looks like this (File's Owner is MyWindowController):
I implemented + (NSSet *)keyPathsForValuesAffecting<key> (below), but the binding never updates, even when the array controller's total count and selection change.
(Additional troubleshooting performed) I had originally been using the Display Pattern Value binding of the NSTextField, but I needed more complicated logic than that binding afforded. I then started listening to the selection changed/changing events of the TableView that displays the array controller's contents and changing the Display Pattern Value bindings dynamically, but that felt like a hack, and overly complicated.
I'm sure there's something I'm missing, but I can't tell what. Does anyone have any ideas? I've read through Apple's key-value-observing documentation, and this seems to be all that's necessary. I've checked, and my keyPathsForValuesAffectingMyString is getting called, but myString only gets called once. I've distilled my code below (updated x3).
Update 1/21
I'm still plugging away trying to figure this out. When I addObserver to self for the arrayController key paths, the notifications do fire as expected, so my key paths and the key value observing mechanism is fine. When I call [self didChangeValueForKey:#"myString"]; within my observeValueForKeyPath method for the same keys, the binding still doesn't update, leading me to believe it's a bindings problem rather than a KVO problem. I'm going to be reading up on the bindings mechanism more...
#interface MyWindowController : NSWindowController {
IBOutlet NSArrayController *arrayController;
}
- (NSArrayController *)arrayController;
- (NSString *)myString;
#end
#implementation MyWindowController
+ (NSSet *)keyPathsForValuesAffectingMyString {
return [NSSet setWithObjects:
#"arrayController.arrangedObjects",
#"arrayController.selection",
nil];
}
- (NSArrayController *)arrayController {
return arrayController;
}
- (NSString *)myString {
// Just as an example; I have more complicated logic going on in my real code
return [NSString stringWithFormat:#"%#, %#",
[arrayController valueForKeyPath:#"arrangedObjects.#count"],
[arrayController valueForKeyPath:#"selection.#count"]];
}
#end
I’ve verified this exact same bug. Someone on Cocoabuilder had a guess as to why the bug happens:
http://www.cocoabuilder.com/archive/cocoa/284396-why-doesn-nsarraycontroller-selection-et-al-fire-keypathsforvaluesaffectingkey.html#284400
I can’t speak as to whether this explanation is true, but I certainly can’t get +keyPathsForValues… to work with NSArrayControllers.
I've got a workaround, but I'm not happy about it, since it shouldn't be necessary, and I would still prefer to get the bindings working properly. I won't accept this answer, and will delete it if someone posts an actual fix. </disclaimer>
#interface MyWindowController : NSWindowController {
IBOutlet NSArrayController *arrayController;
IBOutlet NSTextField *fieldThatShouldBeBinded;
}
- (NSString *)myString;
#end
#implementation MyWindowController
- (void)awakeFromNib {
[arrayController addObserver:self
forKeyPath:#"selection"
options:0
context:NULL];
[arrayController addObserver:self
forKeyPath:#"arrangedObjects"
options:0
context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if( object == arrayController )
[fieldThatShouldBeBinded setStringValue:[self myString]];
}
- (NSString *)myString {
return [NSString stringWithFormat:#"%#, %#",
[arrayController valueForKeyPath:#"arrangedObjects.#count"],
[arrayController valueForKeyPath:#"selection.#count"]];
}
#end
Make sure that the arrayController outlet is connected in Interface Builder. I'm guessing that it's nil.
Don't use the #count keyword. Bindings and KVO on array controllers will get updated when the content changes. If that doesn't work, then there is a problem somewhere else.
Another option is to use the display pattern bindings instead of a composite property. Bind Display Pattern Value1 to arrayController.arrangedObjects.#count and Display Pattern Value2 to arrayController.selection.#count, and set the pattern to "%{value1}#, %{value2}#"
I met the same problem and found another way (but it is still workaround).
You have to declare dynamic workaround property. In implementation section, just return new empty object for it. Now, you can KVO this workaround property.
#property(nonatomic,retain) NSArray *workaround;
#dynamic workaround;
- (NSArray *)workaround { return [NSArray array]; } // new *every* time
- (void)setWorkaround:(NSArray *)unused { }
+ (NSSet *)keyPathsForValuesAffectingMyString { return [NSSet setWithObject:#"workaround"]; }
To get this work, you still need to manually bind self.workaround to arrayController.selectedObjects (or whatever):
- (void)awakeFromNib // or similar place
{
[super awakeFromNib];
[self bind:#"workaround" toObject:arrayController withKeyPath:#"selectedObjects" options:nil];
}
Manual binding works as expected, workaround is updated with what you have bound it to. But KVO tests whether property value is really changed (and stops propagating if it is the same). If you return new self.workaround value every time, it works.
Warning: never call -[setWorkaround:] by yourself — this will effectively flush the other side of binding (arrayController.selectedObjects in this case).
This method has some benefits: you avoid centralized observeValueForKeyPath:... and your logic is in the right place. And it scales well, just add workaround2, 3, and so on for similar cases.
Ok, I'm very new to Obj-C and Cocoa, but I'm sure my bindings here are correct. I've been googling, searching stack overflow and have checked my values again and again.
So, here are my bindings:
They connect to this class:
#interface TMMaddMangaWindowDelegate : NSWindowController {
...
}
...
#property (copy) NSMutableArray* mangaList;
...
#end
#implementation TMMaddMangaWindowDelegate
...
#synthesize mangaList;
// - (NSMutableArray*) mangaList {
// NSLog(#"mangaList was called!");
// return mangaList;
//}
//- (void) setMangaList:(NSMutableArray *) input{
// NSLog(#"setMangaList was called!");
// [mangaList autorelease];
// mangaList = [input retain];
//}
...
-(void) populateList:(NSArray*)list{
NSMutableArray* newArray = [[NSMutableArray alloc] initWithArray:list];
NSLog(#"Populating List.");
for(NSXMLNode* node in list){
[newArray addObject:node.description];
//[[self mutableArrayValueForKey:#"mangaList"] addObject:node.description];
//NSLog(#"%#", node.description);
}
[self setMangaList:newArray];
[[self chapterListDownloadIndicator] stopAnimation:self];
}
As you can see, I also tried the mutableArrayValueForKey approach, which yielded nothing. I know for a fact mangaList is gaining items.
I've been working on this for a while, and probably made a stupid mistake.
Thanks in advance.
It looks like you are changing mangaList behind the array controller's back. Whenever you are making a change to mangaList you should first call [self willChangeValueForKey:#"mangaList"]; and then [self didChangeValueForKey:#"mangaList"]; once you are done with the change. this will let the array controller know it needs to take a look at what changed.
It turns out that the problem was that the window did not have the class identity of Files Owner set to my window controller/delegate. The moment I set this the window sprang to life.
That problem was also preventing my NSProgressIndicator from working.