iOS视图控制器未发布
本文关键字:控制器 视图 iOS | 更新日期: 2023-09-27 18:28:07
(我使用的是c#/Xamarin,但怀疑问题是否特定于此。)
当我添加一个视图控制器,然后删除它时,它的DidReceiveMemoryWarning仍然被调用(在模拟器和实际设备上),所以它不可能被释放。我把范围缩小到这个:-
UIViewController vc=(UIViewController)this.Storyboard.InstantiateViewController(identifier);
this.AddChildViewController(vc);
vc.RemoveFromParentViewController();
vc=null;
调用DidMoveToParentViewController和WillMoveToParentView Controller(如文档中所述)也没有帮助:
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!
有什么想法吗?
可能是视图控制器的初始化方法有一些副作用,导致它保持活动状态。一个常见的例子是,它创建一个NSTimer
对象,该对象保留其目标。浏览从情节提要实例化视图控制器时调用的方法,看看是否有任何东西保留了它。