2013-08-16 60 views
1

(我使用C#/ Xamarin,但懷疑的問題是具體到這一點。)iOS視圖控制器未被髮布?

當我添加一個視圖控制器,然後將其刪除 - 這是DidReceiveMemoryWarning仍然被稱爲(在模擬器和真實設備) ,所以它不能被釋放。我已經收窄,這: -

UIViewController vc=(UIViewController)this.Storyboard.InstantiateViewController(identifier); 
this.AddChildViewController(vc); 
vc.RemoveFromParentViewController(); 
vc=null; 

,並呼籲DidMoveToParentViewController和WillMoveToParentViewController(如文檔中所述)並沒有幫助:

UIViewController vc=(UIViewController)this.Storyboard.InstantiateViewController(identifier); 
this.AddChildViewController(vc); 
vc.DidMoveToParentViewController(this); 
vc.WillMoveToParentViewController(null); 
vc.RemoveFromParentViewController(); 
vc=null; 

然後模擬內存警告和VC DidReceiveMemoryWarning即使沒有提及它也會被調用。當它作爲子控制器被刪除並且沒有對其進行引用時,這是如何實現的。當我使用在故事板中設置的segue轉到UINavigationController中的詳細視圖時,例如,在回到根控制器後,細節控制器仍然收到DidReceiveMemoryWarning消息時,也發生了同樣的情況。

任何幫助瞭解這將不勝感激

UPDATE: 現在我的問題是嵌入在一個UINavigationController

我添加了導航控制研究一個簡單的UIViewController ER:

this.nc=(UINavigationController)this.Storyboard.InstantiateViewController("NavigationController"); 
this.AddChildViewController(this.nc); 
this.nc.DidMoveToParentViewController(this); 

和以後刪除(它的加載後):

this.nc.WillMoveToParentViewController(null); 
this.nc.RemoveFromParentViewController(); 
this.nc=null; 

而這一切工作正常(它不保留)。但是如果我在ViewController的ViewDidLoad方法中添加這個簡單的行,那麼ViewController IS就會被保留!

Console.WriteLine("this.NavigationController={0}",this.NavigationController); 

即,只是訪問「this.NavigationController」導致VC被保留!

所以每次我運行它,我得到另一個ViewController保留!

任何想法?

+0

這將有助於添加c#標記,因爲它與Obj-C語法不同(即Obj-C中的「this。」是「self」)。 – CaptJak

回答

2

它可能是你的視圖控制器的初始化方法有一些副作用,導致它保持活着。一個常見的例子是它會創建一個NSTimer對象,該對象保留其目標。瀏覽從故事板實例化視圖控制器時調用的方法,並查看是否有任何東西保留它。

+0

那麼,我確實有一個VC與System.Timers.Timer在一個案件,並確實修復它 - 非常感謝!我必須停止它,刪除事件並將其設置爲空以最終釋放它。我還有另一個不會發布的VC,並且沒有計時器或任何我能看到的東西。任何其他偉大的想法?有沒有辦法找出爲什麼保留?這是一個以UIViewController作爲根vc的UINavigationController。 – Bbx