簡體   English   中英

iOS View Controller尚未發布?

[英]iOS View Controllers not being released?

(我正在使用c#/ Xamarin,但懷疑該問題是否是特定於此的。)

當我添加一個View Controller並將其刪除時-它的DidReceiveMemoryWarning仍被調用(在Simulator和真實設備中),因此無法發布。 我將其范圍縮小到:

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消息。

任何幫助理解這一點將不勝感激

更新:現在我遇到的問題是嵌入在UINavigationController中的簡單UIViewController

我添加了導航控制器:

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!

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

即,僅訪問“ this.NavigationController”將導致保留VC!

因此,每次運行它時,都會保留另一個ViewController!

有任何想法嗎?

可能是您的視圖控制器的初始化方法有一些副作用,導致它保持活動狀態。 一個常見的示例是它創建一個NSTimer對象,該對象保留其目標。 查看從情節提要中實例化視圖控制器時調用的方法,看看是否有保留的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM