在我的屏幕里,我通过不同的步骤添加了不同的组件,如按钮,标签等。现在点击后我想删除所有内容并显示另一个视图。 这是一些代码:
-(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];
}
使用此代码删除所有内容,但不显示我的新视图的内容,即图形。
答案 0 :(得分:3)
问题在于:
[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];
}
}
答案 1 :(得分:1)
使用
获取当前视图控制器的子视图[self.view subviews];
这将返回一个子视图数组。您可以循环浏览它们,也可以调用每个视图:
[currView removeFromSuperview];
当你循环时,currView将是对子视图的引用。