具有多个视图的UIViewcontroller

时间:2012-06-11 15:58:53

标签: objective-c ios uiviewcontroller dealloc multiple-views

该项目目前有一个名为“Dashboard”的UIviewController,它充当所有应用程序的主视图。这个主视图由两个子视图组成,有点像splitview。主(左视图)的左侧有多个按钮。右侧(右侧视图)将显示左侧所选按钮的内容。

按下按钮时,它将创建一个新的视图实例,如下所示:

vcMySchedule_iPad *vcSchedule = [[vcMySchedule_iPad alloc] initWithNibName:@"vcMySchedule_iPad" bundle:nil];
ncDashboard = [[UINavigationController alloc] initWithRootViewController:vcSchedule];

ncDashboard.navigationBar.barStyle = UIBarStyleBlackOpaque;

ncDashboard.view.frame = self.vwRightPanel.bounds;

[self.vwRightPanel addSubview:ncDashboard.view];

问题是,当按下另一个按钮时,它将显示另一个视图,但前一个被调用的内存仍然存在,并且前一个视图的dealloc永远不会被调用。

我没有使用分割视图,因为左侧有一个按钮,当它预先设置它将左侧移动到左侧而右侧将移动左侧以完全查看。

对此有什么办法吗?

更新了一些图片......

Main(MainViewController): enter image description here

按下课程目录:

vcCourseCatalog_iPad *vcCourse = [[vcCourseCatalog_iPad alloc] initWithNibName:@"vcCourseCatalog_iPad" bundle:nil];
ncDashboard = [[UINavigationController alloc] initWithRootViewController:vcCourse];

ncDashboard.navigationBar.barStyle = UIBarStyleBlackOpaque;

ncDashboard.view.frame = self.vwRightPanel.bounds;

[self.vwRightPanel addSubview:ncDashboard.view];

enter image description here

从表格中选择一行时,它会显示详细信息,如果用户按下该按钮,则会显示最多的视图

enter image description here

我想我可能一直认为新观点错了。物品在哪里被释放?

2 个答案:

答案 0 :(得分:0)

如果没有更多信息,我无法提供可靠的建议,但请检查以下内容:

  1. 你在使用ARC吗?如果,请记住,必须在解除某些内容之前显式释放所有引用。
  2. 你是否继续在其他任何地方提到子视图?如果你仍然在某处(特别是在ARC中)引用它,它会坚持下去。循环引用在这里是邪恶的。
  3. 您是否在使用新视图替换子视图之前将其从超级视图中删除?你会感到惊讶的是它经常是这么简单。
  4. 修改

    在回答下面,关于你没有使用ARC,很明显1)是你的问题。您没有发布参考文献。在这种情况下,这里似乎很明显:

    vcCourseCatalog_iPad *vcCourse = [[vcCourseCatalog_iPad alloc] initWithNibName:@"vcCourseCatalog_iPad" bundle:nil]; ncDashboard = [[UINavigationController alloc] initWithRootViewController:vcCourse];
    
    ncDashboard.navigationBar.barStyle = UIBarStyleBlackOpaque;
    
    ncDashboard.view.frame = self.vwRightPanel.bounds;
    
    [self.vwRightPanel addSubview:ncDashboard.view];
    

    您正在分配vcCourseCatalog_iPadUINavigationController,而不会释放它们。最理想的情况是,您可以自动释放vcCourseCatalog_iPad,并在交换时释放导航控制器。

    你的代码应该是这样的:

    vcCourseCatalog_iPad *vcCourse = [[[vcCourseCatalog_iPad alloc] initWithNibName:@"vcCourseCatalog_iPad" bundle:nil] autorelease];
    
    if(ncDashboard)
    {
        //do any sort of removal from views here
        //[ncDashboard.view removeFromSuperview];
        [ncDashboard release];
    }
    ncDashboard = [[UINavigationController alloc] initWithRootViewController:vcCourse];
    ncDashboard.navigationBar.barStyle = UIBarStyleBlackOpaque;
    ncDashboard.view.frame = self.vwRightPanel.bounds;
    
    [self.vwRightPanel addSubview:ncDashboard.view];
    

答案 1 :(得分:0)

除了CrimsonDiego的回答,我建议您在文件中使用以下行:

.h文件中:

@property (nonatomic, retain) UIView *ncDashBoard;

.m文件中:

@synthesize ncDashBoard = _ncDashBoard;

然后从那时开始使用_ncDashBoard。这是为了确保正确设置保留计数。