Binding NSArrayController to model array without NSTableView: Master-Detail interface
Binding NSArrayController to model array without NSTableView: Master-Detail interface
- Subject: Binding NSArrayController to model array without NSTableView: Master-Detail interface
- From: Fred Glover <email@hidden>
- Date: Wed, 9 Feb 2005 19:24:57 -0800
OK group, I'm stumped.
I've reduced my application to the simplest test case I can think of to solve the problem.
My test project model consists of an array of Model objects. The Model class has two ivars that I want to display in a Master-Detail interface. The Model class has KVC accessors/setters.
@interface Model : NSObject {
NSString *name;
NSNumber *index;
}
- (id)initWithNameAndIndex:(NSString *)aName index:(NSNumber *)anIndex;
- (NSString *)name;
- (void)setName:(NSString *)aName;
- (NSNumber *)index;
- (void)setIndex:(NSNumber *)anIndex;
@end
This is a Document based app. MyDocument contains only one ivar, the modelArray for the collection of Model objects.
@interface MyDocument : NSDocument
{
NSMutableArray *modelArray;
}
- (NSMutableArray *)modelArray;
- (void)setModelArray:(NSMutableArray *)aModelArray;
@end
In the test project I automatically create a small number of Model objects and add them to modelArray in
windowControllerDidLoadNib"
The Window has a NSPopUpButton to select the desired Model object and then various views to display/edit the Model ivars.
The test case has only one instance of MyDocument.
All the examples of NSArrayController bindings include an NSTableView. This usually makes sense. I've got the 1% case.
I don't have and don't need an NSTableView.
I've setup MyDocument nib as follows:
There is only one NSWindow
File's Owner:window outlet is connected to the NSWindow Window. The Window's delegate is MyDocument.
I have an NSArrayController named ModelController. Its content array is bound to MyDocument: modelArray.
Its class is Model.
It has keys "name" and "index" corresponding to the Model ivars of interest. name is an NSString, index is an NSNumber
its content is NOT connected.
I have an NSPopUpButton in the document window. Its content is bound to ModelController: arrangedObjects.
Its contentValues is bound to ModelController: arrangedObjects.name
Its selectedObject is bound to ModelController: selection
I have two NSTextFields.
name:value is bound to ModelController: selection.name.
index:value is bound to ModelController: selection.index
I've implemented the indexed accessors in MyDocument
#pragma mark ------- KVO for bindings -- modelArray
- (unsigned int)countOfModelArray
{
return [modelArray count];
}
- (unsigned int)index
{
return [modelArray objectAtIndex:index];
}
- (id)anObject inModelArrayAtIndex:(unsigned int)index
{
[modelArray insertObject:anObject atIndex:index];
}
- (unsigned int)index
{
[modelArray removeObjectAtIndex:index];
}
- (unsigned int)index
withObject:(id)anObject
{
[modelArray replaceObjectAtIndex:index withObject:anObject];
}
When I run it, the NSPopUpButton gets populated with the modelArray.name values except there's a bogus cell at the bottom of the button labeled: <_NSControllerObjectProxy: 0x347eb0>
When I select one of the valid Model names in the popupbutton, this bogus cell vanishes and my selected cell remains selected. Then the app throws this exception:
#0 0x90a5c638 in -[NSException raise]
#1 0x90a8fff0 in -[NSObject(NSKeyValueCoding) setValue:forKey:]
#2 0x90a900fc in -[NSObject(NSKeyValueCoding) setValue:forKeyPath:]
#3 0x931b8db8 in -[NSBinder _setValue:forKeyPath:ofObject:atIndex:validateImmediately:raisesForNotApplicableKeys:error:]
#4 0x931bb518 in -[NSBinder setValue:forBinding:error:]
#5 0x931c4db4 in -[NSValueBinder _applyObjectValue:forBinding:handleErrors:]
#6 0x931c4f34 in -[NSValueBinder applyDisplayedValueWithHandleErrors:]
#7 0x931c576c in -[NSValueBinder performAction:]
#8 0x931abe38 in -[_NSBindingAdaptor _objectDidTriggerAction:bindingAdaptor:]
#9 0x92f48694 in -[NSPopUpButtonCell _popUpItemAction:]
#10 0x92e78224 in -[NSApplication sendAction:to:from:]
#11 0x92eada44 in -[NSMenu performActionForItemAtIndex:]
#12 0x92ef22e4 in -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:]
#13 0x92f38280 in _NSPopUpCarbonMenu1
#14 0x92f48258 in -[NSCarbonMenuImpl popUpMenu:atLocation:width:forView:withSelectedItem:withFont:]
#15 0x92f47f10 in -[NSPopUpButtonCell trackMouse:inRect:ofView:untilMouseUp:]
#16 0x92e8f010 in -[NSControl mouseDown:]
#17 0x92e03178 in -[NSWindow sendEvent:]
#18 0x92df583c in -[NSApplication sendEvent:]
#19 0x92dfdc54 in -[NSApplication run]
#20 0x92eba2b8 in NSApplicationMain
#21 0x000e5f74 in main at main.m:13
I've tried every way I can think of to make this simple test work and I'm at a loss.
So does NSArrayController require an NSTableView to work properly?
What have I missed connecting in IB?
Do I have to implement my own controller inherited from NSObject?
Fred
Fred Glover
Machine Vision and Image Processing Engineering
Visicon Inc
www.visiconeng.com
Los Gatos, CA
408 354-0095
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden