1

(我使用的是 c#/Xamarin,但怀疑这个问题是特定的。)

当我添加一个视图控制器然后删除它时 - 它的 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 也会被调用。当它作为子控制器被删除并且没有引用它时,这怎么可能。

(当我使用 Storyboard 中设置的 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!

有任何想法吗?

4

1 回答 1

2

可能是您的视图控制器的初始化方法有一些副作用,导致它保持活动状态。一个常见的例子是它创建了一个NSTimer对象,该对象保留了它的目标。浏览从情节提要中实例化视图控制器时调用的方法,看看是否有任何东西保留它。

于 2013-08-17T02:06:07.507 回答