如何删除在当前视图上可见的所有内容,然后显示其他视图?

问题描述

| 在我的屏幕上,我通过不同的步骤添加了不同的组件,例如按钮,标签等。现在单击后,我想删除所有内容显示一个视图。 这是一些代码
-(void)estimateButtons:(Nsstring *)text andFrameX:(int)x andFrameY:(int)y andFrameW:(int)w andFrameH:(int)h 
{
    estimate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    estimate.frame =CGRectMake(x,y,w,h);
    //[estimate setTitle:text forState:UIControlStatenormal];
    UIImage * buttonImage = [UIImage imageNamed:@\"button_green_estimate.png\"];
    UIImage * strechableButtonImage = [buttonImage stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [estimate setBackgroundImage:strechableButtonImage forState:UIControlStatenormal];
    [estimate addTarget:self action:@selector(estimateSelected:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:estimate];
}

-(void)estimateSelected:(UIButton *)b
{
    [self.view removeFromSuperview];    
    graph = [[SFNDoorBargraphVC alloc]initWithNibName:@\"SFNDoorBargraphVC\" bundle:nil];
    [graph.view setCenter:CGPointMake(350,660)];
    [self.view addSubview:graph.view];
}
使用此代码,它删除了所有内容,但未显示新视图(图)的内容。     

解决方法

问题是这一行:
[self.view removeFromSuperview];    
所以当你:
[self.view addSubview:graph.view];
图视图将添加到不在视图层次结构中的视图中。 尝试这个:
NSArray *sViews = [self.view subviews];
[sViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
编辑:根据请求在注释中添加。
NSArray *sViews = [self.view subviews];
for (UIView *sv in sViews)
{
     if (![sv isEqual:viewToSave])
     {
           [sv removeFromSuperview];
     }
}
    ,使用获取当前视图控制器的子视图
[self.view subviews];
这将返回一个子视图数组。您可以遍历它们,并可以为每个视图调用:
[currView removeFromSuperview];
在您遍历时,currView将是对子视图的引用。