views:

202

answers:

1

So I'm trying to use a UITableViewController (let's call it homeView) in my iPhone application to display a simple table with only a few rows of text which are loaded from an NSArray in the controller. I want to display the table in the grouped style in a subview of a subview (let's call it subSubView) of my main controller. When I try the following: [subSubView addSubview:homeView.view], my app crashes on launch. However, when I allocate the object without adding it to any views, it launches fine.

What's the best way (or rather a working way) to display the table generated by my UITableViewController?

+1  A: 

There isn't enough to tell for sure what is going on, but if I had to guess I would think that you probably aren't retaining homeView. Normally I would say that as a comment to your question, since it is not really an answer, but I have a completely separate answer:

Just use a UITableView, not a UITableViewController. Instead of trying to embed a controller within a controller (which is quite difficult since Apple doesn't expose the necessary tools to actually modify the view controller hierarchy), just make the VC you are writing support the appropriate delegate/dataSource methods and directly create the view.

While it might make some logical sense to try to embed VCs inside of each other, with the exception of the builtin container VCs (UINavigationController, UITabBarController) it Really Doesn't Work™. The technical reason for this is that internally some of the event routing and messaging depends on parentViewController being correct, but since you can't set it (setParentViewController: is private) tons of latent bugs in UIKit start rearing their head. The internal collection classes can set the parentViewController correctly, so everything works right.

Also, one last thing. In your question you named your view controller homeView. Please, please on't do that. A view controller and a view are separate things, call it homeViewController. Aside from the fact that a lot of new iPhone developers get confused about what the distinction is, there is nothing more aggravating then tracing through someone else's code and realizing that something you are assuming is one type is another.

Louis Gerbarg
Thank you very much for the detailed answer, it was very informative. I actually did try something which I think is similar to your suggestion (create a UITableView in IB and hook up the dataSource/delegate to my UITableViewController), but it did not display any data when I ran it, just a striped box. Is this what you were suggesting, or was it something else?Thanks.(P.S. - Noted on the homeView name. I've tried to be good about that but it looks like I slipped)
Arman
I actually don't mean you should hook it up to a UITableViewController (though you could do that in some cases). I mean hook the dataSource and delegate up to your main view controller and implement the appropriate protocol methods there. Also, sorry if I sounded like an ass on the homeView thing, I have certainly made that particular faux pas myself.
Louis Gerbarg