Sunday, October 4, 2009

Bindings: multiple controllers (3)

It's a bit embarassing, but I haven't been able to figure out yet why the previous example doesn't work completely. My usual approach in this situation is to back off, make a simplified version that has no extra elements, and see if I can make that work. I did this, and show the working result here. I don't yet know how it differs from the other one that has a problem.

Make a new Cocoa application project with code for the App Delegate header and implementation as given below. Set up the bindings as shown in the screenshots (the array controllers are called AC1 (for the table column) and AC2 (for the popup)). It works correctly.



AC1 is bound to the App Delegate (the name doesn't show completely):



AC2 is bound to AC1:









header:
#import <Cocoa/Cocoa.h>

@interface PopUpBindings2AppDelegate :
NSObject <NSApplicationDelegate> {
NSWindow *window;
NSMutableDictionary *mD1;
NSMutableDictionary *mD2;
}

@property (assign) IBOutlet NSWindow *window;
@property (retain) NSMutableArray *dataArray;

@end


implementation:
#import "PopUpBindings2AppDelegate.h"

@implementation PopUpBindings2AppDelegate

@synthesize window;
@synthesize dataArray;

- (id)init {
self = [super init];
if (self == nil) return nil;

dataArray = [NSMutableArray
arrayWithCapacity:5];

mD1 = [NSMutableDictionary
dictionaryWithCapacity:3];

[mD1 setObject:[NSArray
arrayWithObjects:@"A",@"B",nil]
forKey:@"items"];
[mD1 setObject:@"X"
forKey:@"name"];
[mD1 setObject:@"A"
forKey:@"selectedItem"];
[dataArray addObject:mD1];

mD2 = [NSMutableDictionary
dictionaryWithCapacity:3];

[mD2 setObject:[NSArray
arrayWithObjects:@"C",@"D",nil]
forKey:@"items"];
[mD2 setObject:@"Y"
forKey:@"name"];
[mD2 setObject:@"C"
forKey:@"selectedItem"];
[dataArray addObject:mD2];

NSLog(@"dataArray %@", dataArray);

return self;
}

@end

No comments: