【发布时间】:2011-01-03 15:52:15
【问题描述】:
我有以下设置:
一个具有 splitView 的窗口,在该窗口中,我在左侧视图中显示 NSCollectionView,在右侧视图中显示 detailView。两个视图都设置在单独的 xib 中。此外,我有一个数据控制器(NSArrayController 类),它管理一个可变的 NSMutableDictionaries 数组(moviesForChoice)。 dataController 设置为应用程序委托。数组中的电影对象具有(名称、情节、流派等)等属性
到目前为止一切都很好......
在 NScollectionview 的 xib 中,我通过 Application.delegate.moviesForChoice 将 NSArraycontroller 内容属性绑定到我的数据控制器。collectionView 访问 arraycontroller.arrrangedObjects 和 arraycontroller.selectionIndexes。这工作正常,内容显示和选择在 collectionview 中工作正常(我的 collectionviewItem 呈现选择颜色)
在 detailView 的 xib 中,我想在 collectionview 中显示所选对象的信息。因此,我还向 xib 添加了一个数组控制器,将内容数组绑定到 Application.delegate.moviesForChoice 并将视图中的 NSTextfields 绑定到例如arraycontroller.selection.name
我的问题来了: 每次我打开带有两个 xib 的窗口时,我的 collectionview 都会正确显示所有可供选择的电影,而 detailview 会显示我的 collectionview 中第一个对象的信息。每当我点击 collectionView res. item 呈现选择颜色,但 detailView 不更新。
我的理解是,DataController 不知道 selectionIndexes 中的更新,因此不能触发 detailView 中的更新。如果我错了,请纠正我...
为了解决这个问题,我尝试将 collectionView xib 中 arraycontroller 的 selectionIndexes 属性绑定到 Application.delegate.moviesForChoice.selecionIndexes 但这失败了: 不支持 addObserver:forKeyPath:options:context:]。关键路径:selectionIndexes
我可以想象这意味着数据控制器不符合我的 Array moviesForChoice 的 KVO,但我为它实现了以下方法:
-(void)insertObject:(NSDictionary *)dict inMoviesForChoiceAtIndex:(NSUInteger)index {
[moviesForChoice insertObject:dict atIndex:index];
}
-(void)removeObjectFromMoviesForChoiceAtIndex:(NSUInteger)index {
[moviesForChoice removeObjectAtIndex:index];
}
-(void)setMoviesForChoice:(NSMutableArray *)a {
moviesForChoice = a;
}
-(NSArray*)moviesForChoice {
return moviesForChoice;
}
-(NSUInteger)countOfMoviesForChoice
{
return [moviesForChoice count];
}
- (void)addMovieForChoiceObject:(Movie *)anObject
{
[moviesForChoice addObject:anObject];
}
那么我哪里错了?如何正确绑定到 selectionIndexes?
非常感谢您的帮助! M
【问题讨论】:
标签: cocoa-bindings nsarraycontroller key-value-observing