views:

36

answers:

1

I was under the impression that adding a subview to a view goes like this:

UITableViewController *sitesel = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
sitesel.view.frame = CGRectMake(0,0,100,100);
[self.left addSubview:sitesel.view];
[sitesel release];

But it seems I should not release the sitesel (the controller)? So should I release the view or what, I had this retain stuff nailed a while ago, but it's slipped. (And to use a TableView, you have to subclass UITableViewController right?)

(self.left is a subview of self.view, added in a nib)

+1  A: 

addSubview does retain the view, that's not the problem. Your issue is that the controller for the view goes away a little later.

You shouldn't release the view, because that's none of your business. You didn't create it, you didn't touch it. Leave it alone.

In order to keep things working, it needs to stay connected to a valid controller. Hence, you must not release the controller, but keep it around. Add a property like @property(retain) UITableViewController *siteController; and then do self.siteController = sitesel; before you release the controller. This way everything stays in memory.

PS: For cleanness, you should probably change the view in the accessor for sitesel. Just to make sure it always comes and goes along the controller. Your method would then get even shorter, just setting the controller.

ADDED: That setter could look like that, requiring you to set only the controller and the view being updated transparently:

- (void)setSiteselController:(UITableViewController *)ctrl {
  if (_sitesel)
    [_sitesel.view removeFromSuperview];

  [_sitesel autorelease];
  _sitesel = [ctrl retain];

  if (_sitesel) {
    _sitesel.view.frame = CGRectMake(0,0,100,100);
    [self.left addSubview: _sitesel.view];
  }
}

Your original code will then shrink to this much cleaner version:

UITableViewController *sitesel = [[UITableViewController alloc] initWithStyle: UITableViewStyleGrouped];
self.siteselController = sitesel;
[sitesel release];

PPS: You don need an controller for a UITableView to work. It's just much simpler!

Max Seelemann
Sorry, could you expand on your PS, I don't quite get. But I don't need to access the UITableViewController again, everything from that point onwards is done in TableViewController itself.
Jonathan
You should override the accessor and add the view in there. Like so: `_controller = newController; _controller.view.frame = ... [self.view addSubview: _controller.view];`That you don't need the controller doesn't matter here. You have to keep it since it goes away if you don't. Also you maybe might want to remove/exchange that view later on. Then it's good to know the controller in order to not wast memory.
Max Seelemann
sorry I still don't understand, I dont see any difference in the code you just posted.
Jonathan
I've adjusted my post with more details.
Max Seelemann