Objective c 绑定到NSCollectionview的NSArrayController
我对绑定有点陌生,不知怎么的阻止了它。但我现在想用它们。 谈到OSX,这是用代码编程的,而不是IB 所以,我有来自CoreData的数据进入我的ArrayController。NSCollectionView已绑定到此arraycontroller,如果存在数据,则此绑定将起作用并显示数据 但是,每个项目都有一些按钮、滑块和文本字段。一次单击,代码就会更改这些内容的标记或值。当我将更改发送到coredata并保存它时,我认为这已经足够了。arraycontroller是否应该获取此信息并在collectionview中更新我的项目 因为标记(我尝试的第一件事)在coredata中更新后不会得到更新 这些领域是否必须以某种方式加以限制 标记在NSCollectionViewItem的子类中设置如下:Objective c 绑定到NSCollectionview的NSArrayController,objective-c,core-data,nsarraycontroller,nscollectionview,Objective C,Core Data,Nsarraycontroller,Nscollectionview,我对绑定有点陌生,不知怎么的阻止了它。但我现在想用它们。 谈到OSX,这是用代码编程的,而不是IB 所以,我有来自CoreData的数据进入我的ArrayController。NSCollectionView已绑定到此arraycontroller,如果存在数据,则此绑定将起作用并显示数据 但是,每个项目都有一些按钮、滑块和文本字段。一次单击,代码就会更改这些内容的标记或值。当我将更改发送到coredata并保存它时,我认为这已经足够了。arraycontroller是否应该获取此信息并在col
[[(BEItem *)[self view] valueSlider] setTag:[[representedObject itemTag] intValue]];
我是否必须告诉CollectionView更新自身并从控制器获取新数据
谢谢
本杰明
编辑
我已更改了我的收藏视图。我读到绑定一个可表示对象实际上是不可能的,在下面的回答中,它绑定到某个属性,但是这个属性也没有更新。然后我了解到newItemForRepresentedObject,您应该使用这个函数。现在,我创建了如下所示的所有内容,但是程序总是在10秒后崩溃,什么也不显示。它不断调用setChannelID,但从未将ID设置为属性。正因为如此,我认为这就是问题所在。(如果永远不会返回,则只返回)
这里有什么问题?到现在为止,我真的被collectionview弄糊涂了。
这只是代码,IB中没有
在appdelegate中设置视图:
NSCollectionViewItem *testitem = [[NSCollectionViewItem alloc] init];
[testitem setView:[ChannelView new]];
self.collectionView = [[ChannelCollectionView alloc] initWithFrame:NSMakeRect(10, 0, mixerWidth, self.splitview.frame.size.height)]; // initWithFrame:[[[self window] contentView] frame]
[self.collectionView setItemPrototype:testitem];
[self.collectionView setMaxNumberOfRows:1];
[self.collectionView setAutoresizingMask:(NSViewMinXMargin | NSViewWidthSizable | NSViewMaxXMargin | NSViewMinYMargin | NSViewHeightSizable| NSViewMaxYMargin)];
[self.collectionView setAutoresizesSubviews:YES];
[self.collectionView bind:NSContentBinding toObject:self.channelController withKeyPath:@"arrangedObjects" options:nil];
ChannelView:
#import <Cocoa/Cocoa.h>
@interface ChannelView : NSView
@property (readwrite, nonatomic, copy) NSString *channelName;
@property (readwrite, nonatomic, copy) NSNumber *channelID;
@property (readwrite) NSTextField *channelNameField;
@property (readwrite) NSTextField *deviceChannelField;
@end
@implementation ChannelView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:NSMakeRect(0, 0, 300, 500)];
if (self) {
// Initialization code here.
ColorView *test = [[ColorView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
self.channelNameField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 20)];
self.deviceChannelField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 50, 100, 20)];
[self addSubview:test];
[self addSubview:self.channelNameField];
[self addSubview:self.deviceChannelField];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
//add die teile
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
}
// setters.
-(void)setChannelID:(NSNumber *)chanID
{
//NSLog(@"hallo");
if (self.channelID == chanID) {
return;
NSLog(@"da");
}
else {
NSLog(@"hello"); //just this in debug output
self.channelID = [chanID copy];
NSLog(@"no output");
// self.channelID = chanID;
NSLog(@"chanid %d current: %d", chanID.intValue, self.channelID.intValue); //never shown in debug
[self.deviceChannelField setStringValue:[NSString stringWithFormat:@"%d",self.channelID.intValue]];
}
}
@end
如果有人知道setter不设置属性的原因,请告诉我:)
它应该能够做到这一点,并且不会发布或任何东西,至少我知道(使用ARC)是的,您必须将滑块的值绑定到CollectionViewItem 您可以使用以下方法在代码中执行此操作:
-bind:toObject:withKeyPath:options:
在您的示例中,它是这样的:
[[(BEItem *)[self view] valueSlider] bind:@"tag" toObject:self withKeyPath:@"itemTag" options:nil];
或者,如果使用IB,则在InterfaceBuilder中,通过设置要绑定到集合视图项的值representedObject.itemTag
或者,我是否必须将字段的每个属性绑定到表示的对象,但据我所知,标记不是可绑定的选项?添加了新代码以显示新问题谢谢,有点帮助,但我现在完全更改了CollectionView。它显示其视图并尝试绑定my textfield,但不知何故未能在itemView中设置属性。
[[(BEItem *)[self view] valueSlider] bind:@"tag" toObject:self withKeyPath:@"itemTag" options:nil];