Note that there are some explanatory texts on larger screens.

plurals
  1. PONSArrayController bound to NSCollectionview
    text
    copied!<p>I'm kinda new into bindings, somehow prevented it. But I want to use them now. Talking about OSX and this is programmed in code not in IB.</p> <p>So, I have data coming from CoreData into my ArrayController. A NSCollectionView is bound to this arraycontroller and if there is data, this binding works the data is displayed. </p> <p>But, each item has some buttons, sliders, textfields. On a click, a code will change the tag or value of those things. I thought it is enough when I send the change to coredata and save it. Shouldnt the arraycontroller get this and update my items in the collectionview?</p> <p>Because the tags(first thing I tried) dont get updated if its updated in coredata.</p> <p>Do those fields have to be somehow bound?</p> <p>the tag is set in a subclass of NSCollectionViewItem this way:</p> <pre><code>[[(BEItem *)[self view] valueSlider] setTag:[[representedObject itemTag] intValue]]; </code></pre> <p>Is there anything I have to tell the CollectionView to update itself and take the new data from the controller?</p> <p>Thanks Benjamin</p> <p><strong>EDIT</strong></p> <p>I have changed my collectionview. I read it isnt really possible to bind a representable object and in the answer below, it is bound to some property, but this property isnt updated either. Then I read about newItemForRepresentedObject that you should use this function. Now, I created everything like shown below, but the program is always crashing after 10 seconds or something and nothing is displayed. It is continuously calling setChannelID, but never setting the ID to the property. Because of that it is always called I think this is the problem. (The if gets never to only return)</p> <p>What is the problem here? Im really confused by the collectionview by now. And this is just code, nothing in IB.</p> <p>Setting up the View in appdelegate:</p> <pre><code>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]; </code></pre> <p>ChannelView:</p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @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 </code></pre> <p>And this piece in my subclasses NSCollectionView</p> <pre><code>- (NSCollectionViewItem *)newItemForRepresentedObject:(ChannelsToMixes*)object { NSCollectionViewItem *item = [super newItemForRepresentedObject:object]; // ChannelView *view = (ChannelView *)[item view]; NSLog(@"cahnnelid: %d",object.channelID.intValue); // [view bind:@"title" toObject:object withKeyPath:@"title" options:nil]; [item.view bind:@"channelID" toObject:object withKeyPath:@"channelID" options:nil]; //NSLog(@"test"); //NSLog(@"%@",object); return item; } </code></pre> <p>If anyone knows why the setter isnt setting the property give me a tip :) It should be able to do this and is not released or anything, at least that I know of (using ARC)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload