NSPopupButton Bindings with Value Transformer

I don’t know if what I see with the pop-up button populated with bindings with a value transformer is what it should be or not, an unusual thing that I see (at least as regards visible with the values ​​of transformers and table views) lies in the fact that the value parameter in the convertedValue: method is the entire array bound to the controller of the array, not individual rows in the array. When I did this with table views, the transformer is called once for each displayed row in the table, and the value parameter is any object attached to this row and column, and not the entire array that serves as the content array for the array controller .

I have a very simple application to verify this. The application delegate has the following:

+(void)initialize { RDTransformer *transformer = [[RDTransformer alloc] init]; [NSValueTransformer setValueTransformer:transformer forName:@"testTransformer"]; } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { self.theData = @[@{@"name":@"William", @"age":@"24"},@{@"name":@"Thomas", @"age":@"23"},@{@"name":@"Alexander", @"age":@"64"},@{@"name":@"James", @"age":@"47"}]; } 

In the RDTransformer class, this is:

 + (Class)transformedValueClass { return [NSString class]; } +(BOOL)allowsReverseTransformation { return NO; } -(id)transformedValue:(id)value { NSLog(@"%@",value); return value; } 

In IB, I added NSPopupButton to the window and the array controller to the list of objects. The controller content array is bound to the App Delegate.theData and the popup button content values ​​are bound to the Array Controller.arrangedObjects.name with the transformer of the testTransformer value.

When I run the program, the log from the transformValue: method is:

 2012-09-19 20:31:39.975 PopupBindingWithTransformer[793:303] ( ) 2012-09-19 20:31:40.019 PopupBindingWithTransformer[793:303] ( William, Thomas, Alexander, James ) 

This is not like other people's experience that I see on SO. Is there something I am doing wrong with bindings or a value transformer?

+4
source share
1 answer

Unfortunately, this is how NSPopUpButton works. The problem is not limited to this control. If you try to associate NSArrayController.contentArray with another NSArrayControllers.arrangedObject.someProperty, you will get the same problem. Here is a simple workaround that I use in all my value transformers, which makes them work with both tables and pop-ups:

You can change your value transformer as follows:

 -(id)transformedArrayValue:(NSArray*)array { NSMutableArray *result = [NSMutableArray array]; for (id value in array) [result addObject:[self transformedValue:value]]; return result; } -(id)transformedValue:(id)value { if ([value isKindOfClass:[NSArray class]]) return [self transformedArrayValue:value]; // Do your normal-case transform... return [value lowercaseString]; } 

This is not ideal, but easy to replicate. I really put transformedArrayValue: in the class, so I don't need to copy it everywhere.

+4
source

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


All Articles