2011-04-12 44 views
0

在我的我有一個屏幕,我通過不同的步驟添加按鈕,標籤等不同的組件。現在點擊後,我想刪除所有內容並顯示另一個視圖。 下面是一些代碼:如何刪除當前視圖中可見的所有內容,然後顯示不同的視圖?

-(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]; 
} 

有了這個代碼的清除一切,但它不是我的顯示新視圖的內容是圖。

回答

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]; 
    } 
} 
+0

工作正常!非常感謝。有沒有辦法從刪除中保存視圖,但不是休息。 – Ashutosh 2011-04-12 21:24:14

1

使用

[self.view subviews]; 

這將返回子視圖數組獲取當前視圖控制器的子視圖。你可以遍歷它們併爲每個視圖,您可以撥打:

[currView removeFromSuperview]; 

其中currView將是子視圖的引用作爲您的循環通過。

+0

可否再詳細一點。 – Ashutosh 2011-04-12 21:20:32

+0

另外問題不在於刪除,而是顯示新視圖。 – Ashutosh 2011-04-12 21:21:51

+0

我得到了數組,但不能通過循環來刪除所需的數組。上面的答案讓我刪除所有內容並顯示我的內容。但是現在我想刪除特定的那些,並保留其餘部分。我應該怎麼做? – Ashutosh 2011-04-12 22:04:05

相關問題