Object created in Interface Builder getting dealloc'ed too soon

Posted by Collin Allen on Stack Overflow See other posts from Stack Overflow or by Collin Allen
Published on 2010-04-10T06:22:58Z Indexed on 2010/04/10 6:33 UTC
Read the original article Hit count: 379

Filed under:
|
|
|
|

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!

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about cocoa-touch