views:

199

answers:

1

The Project

I'm working on a relatively simple iPhone OS project that's navigation controller based, with a root table view and a detail table view. Tap an item in the main list to see its details in a pushed table view.

The Setup

I broke out the data source for both views into their own objects so as not to muddy the purpose of a view controller. Having done this, the table views no longer have data sources since those methods are now in separate files, so I created an instance of each data source class in the appropriate XIB files with the Object item (dragged it in, then set its class). Then, to actually connect the tableviews to their data sources, I set the dataSource outlet of each tableview to the yellow data source object in Interface Builder. The table view delegates are still set to their view controllers.

The Problem

The root table view works just fine, but when you tap a row to push to the detail view, the data source object gets instantiated as expected, then immediately dealloc'ed, causing a crash (numberOfSectionsInTableView: gets called on the freed object). I can't figure out why the data source is getting automatically dealloc-ed when I need it right then and there for the detail view, as indicated by my data source object creation and tableview connection in Interface Builder. What's more perplexing is that the very approach works fine for the root tableview!

The Question

Is there anything obvious I'm missing that would cause this to happen? Or, is this even the right way to instantiate a data source for a table view controller? It seems like poor object oriented programming to do it from within the view controller, which should only be concerned with the view. I could cram everything in two table view controller classes and it would probably work, but it would not be as modular as I'd like.

Thanks!

+1  A: 

What does the property for your detail-table-view-controller data-source look like? Are you calling retain on it? The problem may be that you have:

@property (nonatomic, assign) IBOutlet id <UITableViewDataSource> dataSource

Instead of:

@property (nonatomic, retain) IBOutlet id <UITableViewDataSource> dataSource
Kevin Sylvestre
That's probably part of my problem right there... I'm just using the dataSource outlet of the standard UITableView in the XIB (File's Owner is the data source class, not the table view itself). Come to think of it, there is no memory management for my data source! Even with that property added, what's the next step? There's nothing to change in IB that I can see.
Collin Allen
Do you have a referencing outlet setup to the data source? Just to clarify, how did you add your UITableViewDataSource conforming object to interface builder?
Kevin Sylvestre
I've got no outlets set up in code, just the dataSource connection on the tableview to the data source object in IB.To create the instance of my data source, I dragged an Object out of the Library panel and set its Class to my data source class. Then I connected the tableview's dataSource to that object.
Collin Allen
OK. Try adding the second property from the example. Then in interface builder drag it over as a new referencing outlet. Se if the data source is retained.
Kevin Sylvestre
Ah, that did it! I made my data source instance in IB be the dataSource outlet of my view controller class ("owned" and memory-managed by that class I guess you could say), while leaving it as the dataSource for the tableview. I also put [dataSource release] in my view controller's dealloc() to free it when I'm through with it. Thanks!
Collin Allen