views:

239

answers:

1

Hi, I have a simple Core Data app I am building to try to understand Core Data. It has two entities: a weather station, and a collection of observations for a given station.

I created the initial interface for this by putting a tab view on my window, selecting the first tab, and dragging the weather station entity onto that view; then selecting the second tab and dragging the observations entity onto the second tab.

I then created a third tab myself and added a popup at the top. This popup is bound to the StationArrayController, so it populates with all the weather stations I add. This works great.

I then added a table view to display the Observations associated with the selected Station. This also works great. I implemented this with a method that creates a predicate that searches for all observations whose station matches the selected station, and attached the predicate to the ObservationArrayController associated with the Table View on the third tab. (I couldn't figure out how to do this in IB, so I did it programmatically).

The bug is this: If I load a previously saved file with weather stations and observations in it, when I go to the third tab, even though one of the stations is selected (ie, it appears in the popup), all of the observations appear in the tableview, not just the ones associated with that station.

This is happening because I am not calling the method to attach the predicate to the Observation Array Controller until I actually use the popup to change the station manually.

So my problem and question is this (sorry it took so long to get to this point!): How can I detect when the tab view has loaded so I can force the method which attaches the predicate to run and thus have an initial list of Observations which matches the selected Station?

I tried creating a custom View class and subclassing it for the third tab view and putting this code in the awakeFromNib method, but this is too early in the process (the "selected station" is -1 at this point in the loading process).

Any help would be much appreciated. thank you!!

Elisabeth

A: 

Ah, I just answered my own question!

I set the delegate of the tabview to MyDocument, and implemented tabView:didSelectTabViewItem: there. I created an IBOutlet in MyDocument pointing to the third tab view, and in this method, checked to see if the third tab was selected, and if so, called the method to attach the predicate to the ObservationArrayController. Works great!

Elisabeth
Sounds like the predicate is firing before the array controller has gotten all its contents, thus at the time it's queried with the predicate it doesn't have anything.
ctshryock
@ctshryock - well in my case, the predicate wasn't yet attached to the ObservationArrayController, since that was only happening when I actually selected something from the popup menu. So if all I did was select that tab, then that code wasn't being called.
Elisabeth