2012-09-26 128 views
1

我有我的故事板與出口mynewView UIView,我希望它顯示完全不同的內容。並做很多次。我可以重用UIView嗎?

我的UIView包含很多對象,我從互聯網上加載它。但每次我做

NSArray *viewsToRemove = [view1 mynewView]; 
    for (UIView *v in viewsToRemove) { 
     [v removeFromSuperview]; 
    } 

    //Let's try to remove all possible views.. 

    viewsToRemove = [self.view subviews]; 
    for (UIView *v in viewsToRemove) { 
     [v removeFromSuperview]; 
    } 

    //here i create mynewView 
    mynewView = [self createnewRandomView]; 
    //And finally add the View we have created. 

    [self.view addSubview:mynewView]; 

我監視內存消耗,我看到它增長每次我添加新的UIView。即使我刪除所有超級視圖。我想他們仍然留在記憶中。

在我更改視圖之前,我從超級視圖中刪除了所有可能的視圖,但它們仍然保留在內存中。

Xcode設置爲使用ARC,因此我無法釋放它。

有沒有什麼好的方法來重用UIView?

感謝

回答

-2

當你刪除子視圖,它們設置爲nil,當您去,就像這樣:

for (UIView *v in viewsToRemove) { 
    [v removeFromSuperview]; 
    v = nil; 
} 

欲瞭解更多信息,請參見例如Arc: Setting references to nil, for multiple buttons

+1

你真的應該閱讀鏈接的文章 - 注意「NO」雙組分,並再次檢查你的答案。 – Till

+0

我錯過了原始提問者並不持有對子視圖的引用的事實,對不起。那麼你的答案是什麼? –

0
[self.view addSubview:mynewView]; 

你NewView的被添加爲self.view的子視圖。 您的上述代碼不會用於在self.view中刪除您的VewView。 請添加新視圖之前嘗試此代碼:

for (UIView *view in self.view) { 
    if([view isKindOfClass:[YourNewViewClassName class]]) 
    { 
     [view removeFromSuperView]; 
     break; 
    } 
}