On Dec 8, 2008, at 04:18, Steven Hamilton wrote:

I have a custom NSWindowController called budgetController. Interface thus (cropped a bit)

@interface MLBudgetController : NSWindowController {

        IBOutlet NSArrayController *incomeEnvelopeController;
        NSManagedObject *selectedIncomeEnvelope;
}

My window has an NSPopupButton bound to my NSArraycontroller *incomeEnvelopeController. The selectedObject binding is bound to the selectedIncomeEnvelope above. The controller sources Core Data for my "Envelope" entities and on running, it fetches and displays the set of entities fine showing the correct values. It also sets the selectedIncomeEnvelope fine too.

But on first load I have a "No Value" in the popup. Selecting an entry removes the No Value from the list. In my budgetController's windowDidLoad{} function I'm attempting to set the selectedIncomeEnvelope to the first object in the array like so;

selectedIncomeEnvelope= [[incomeEnvelopeController arrangedObjects] objectAtIndex:0];

However, the arrangedObjects array, while existing, contains zero objects. I've checked the Outlet is connected ok. A timing issue of some sort?

No. Based on the above, your code isn't KVO compliant.

First of all, it's important to realize that nothing is ever bound to an instance variable (selectedIncomeEnvelope), but to an object property (MLBudgetController instance, key "selectedIncomeEnvelope"). The two are not the same thing, although KVO will *pretend* there's a property when there's only a variable (which appears to be your case, and which is why it works some of the time).

When you go to set the initial selected object, you need to change the property, not the variable. This is easiest if you actually define the property (or write the getter & setter):

        @property NSManagedObject *selectedIncomeEnvelope;
        ...
        @synthesize selectedIncomeEnvelope;
        ...
self.selectedIncomeEnvelope= [[incomeEnvelopeController arrangedObjects] objectAtIndex:0];

That should cause the proper KVO notification to be sent, and for the correct object to be selected in the user interface.

Or, if you don't want to define the property explicitly, then you have to generate the notification yourself:

        [self willChangeValueForKey: @"selectedIncomeEnvelope"];
selectedIncomeEnvelope= [[incomeEnvelopeController arrangedObjects] objectAtIndex:0];
        [self didChangeValueForKey: @"selectedIncomeEnvelope"];


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to