views/subviews memory issue...

Discussion in 'iOS Programming' started by MACloop, Mar 12, 2010.

  1. MACloop macrumors 6502

    May 18, 2009
    I intend to use a tableView in an App. When the user clicks on a cell, antoher viewcontroller is created and it slides in from right. This works fine.

    The problem is:
    - The view in this viewcontroller created must include 2 subviews. One map and one table. When the view is loaded, the table should be displayed. When clicking on a segmented control a switching between the two subviews is enabled. So, how should I add those subviews? They are objects of UIViewController subclasses defined by me and my problem is to know how and when I shall create those views. Everytime the viewcontroller is created ie. the user click on the initial tabel cell, the init and the ViewDidLoad method are called. This means that if I create and add those subviews in one of those methods, they will be created everytime...? Is that good? I know that they may be relased and the classes may be cleaned up when the user leave them, but somehow I do not have a good feeling on creating them over and over again...? I would add the subviews in ViewDidLoad or in the init-method like this:
    VC *vcTable = [[VC alloc]initWithNibName:@"VCTablenib" bundle:nil]);
    self.table = vcTable.view;//table is retained and @synthezised
    [vcTable release];
    [self.view addSubview:table];
    //add some data to this table
    //perhaps reload it again
    VC *vcMap = [[VC alloc]initWithNibName:@"VCMapnib" bundle:nil]); = vcMap.view;//map is retained and @synthezised
    [vcMap release];
    [self.view addSubview:map];
    //call method for the map to add annotations on map
    Code for my segmented control:
    - (void)segmentedControlValueChanged {
    	selectedUnit = (NSUInteger*)mapOrList.selectedSegmentIndex;
    	[self updateView];
    - (void)updateView {
    	if (selectedUnit == (NSUInteger*)0) {
    		[map setHidden:YES];
    		[table setHidden:NO];
    	if (selectedUnit == (NSUInteger*)1) {
    		[map setHidden:NO];
    		[table setHidden:YES];

    Is this a good way to solve this???
    If not - how do you do this in a proper way?
    Thanks in advance!
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    If you are using a nib for this view controller then simply create the two views in that nib. Your use of two other nibs makes no sense.

    If I understand you the two views will not be visible at the same time. That's fine. You can hide/show the views if you like. Or you can make both views top level objects in the nib if you like.

Share This Page