Question

I’m trying to create a view-based NSTableView, populated via Cocoa bindings.

My NSArrayController is pointing to the jobs array property of the currentUser property of the application delegate, like so:

binding 1

I expect each model in that array to be of class HSJob:

binding 2

However, once I’m binding my table view to the NSArrayController, its arrangedObjects property for some reason thinks it belongs to class HSJob (I’m expecting an NSArray).

binding 3

Obviously, this doesn’t let me link individual cell content to Job object properties and I can’t display anything in my table view:

binding 4

So what am I doing wrong here?

Update

I even decided to go as far as implementing some manual value update notifications + moving everything into the AppDelegate, just to experiment. Here, I changed my own HSJob class for just some NSDictionaries.

AppDelegate.h

@property NSArray *things;

AppDelegate.m

@synthesize things = _things;

- (NSArray *)things {
    if (!_things)
        return @[@{@"name": @"Default Thing"}];
    else
        return _things;
}

- (void)setThings:(NSArray *)things {
    [self willChangeValueForKey:@"things"];
    _things = things;
    [self didChangeValueForKey:@"things"];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    [self setThings:@[@{@"name": @"Actual Thing"}]];

}

Only “Default Thing” ever gets displayed. Even though I’m setting a new value to the things property right after the application launch, that array never gets displayed.

Was it helpful?

Solution 2

I managed to fix this by binding my NSArrayController to Application instead of the manually created AppDelegate object. Therefore, the Model Key Path is now:

self.delegate.currentUser.jobs

Obviously this doesn’t get autocompleted by Xcode and it still complains about not being able to resolve the path, but it works anyway.

The reason why my AppDelegate object in the Xib wasn’t working is still a mystery, but it may have something to do with the fact that the Xib in question wasn’t the default MainMenu.xib, but a custom NSPanel attached to a controller.

OTHER TIPS

What you "did wrong" was to create an app delegate object in a XIB other than MainMenu.xib, and then assume that it's [NSApp delegate]. What you actually get is a totally separate object of your app delegate class with its own properties, just for that xib. So the object attached to your XIB never runs applicationDidFinish launching because it is not your app's delegate.

I suggest a solution by modifying the init for your app delegate class here: https://stackoverflow.com/a/22873562/3120884

MainMenu.xib gets away with having an App Delegate object because as the NSMainNib designated in Info.plist, it gets special treatment in NSApplicationMain, which does hook up that Interface Builder object to [NSApp delegate].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top