简体   繁体   English

在执行Push Segue时是否应该释放UIViewController

[英]Should a UIViewController be deallocated when it performs a Push Segue

I have a LoginViewController (UIViewController) that when all the criteria is met and the user hits the Login button, a storyboard segue is run that pushes the ProfileViewController (UIViewController). 我有一个LoginViewController(UIViewController),当满足所有条件并且用户单击“登录”按钮时,将运行一个情节提要脚本,以推送ProfileViewController(UIViewController)。 When this happens, I have a log statement in my LoginViewController's dealloc method to see if it is called and to my disappointment it is never called. 发生这种情况时,我在LoginViewController的dealloc方法中有一条日志语句,以查看是否调用了该语句,但令我失望的是,它从未调用过。 My question is whether or not it is supposed to be called? 我的问题是是否应该调用它? Also, when I log in, sometimes I get a "Received memory warning" and sometimes I do not which I find strange because I am taking the exact same steps in both cases and yet i get a memory warning one time and not with the other. 另外,当我登录时,有时会收到“收到的内存警告”,有时却不会,因为我在两种情况下都采取了完全相同的步骤,但是一次却又一次却没有收到内存警告。

Anyone can shine some light on this that would be great! 任何人都可以在此上发光一下,这太棒了!

Thanks. 谢谢。

UINavigationController maintains a stack of view controllers. UINavigationController维护一堆视图控制器。 You start with one element, a LoginViewController , on that stack. 您从该堆栈上的一个元素LoginViewController开始。 When you push a ProfileViewController , you now have two elements on the stack. 当您推送ProfileViewController ,现在在堆栈上有两个元素。 The LoginViewController can't be deallocated until it is removed from the stack. 在从堆栈中删除LoginViewController之前,无法对其进行释放。

If you want the ProfileViewController to replace the LoginViewController on the navigation controller's stack, you can write a custom segue class to implement that behavior. 如果希望ProfileViewController替换导航控制器堆栈上的LoginViewController ,则可以编写一个自定义segue类来实现该行为。 See this Q&A . 请参阅此问答

(You might think you could use the “Replace” or “Show Detail (eg Replace)” segue type in your storyboard, but those only work if you are using a UISplitViewController .) (您可能会认为可以在情节UISplitViewController使用“替换”或“显示详细信息(例如,替换)” segue类型,但这些类型仅在使用UISplitViewController 。)

With ARC enabled, when a object is not referenced, it will be released. 启用ARC后,当不引用对象时,它将被释放。

In order to display view from ProfileViewController , you instantiate a object of it in LoginViewController , and that's how you still can see the profile view after it is presented. 为了显示ProfileViewController视图,您可以在LoginViewController实例化该对象的一个​​对象,这就是在显示配置文件视图后仍然可以看到它的方式。 If LoginViewController instance is released, the profile view will also get released(assume no one else references it). 如果释放LoginViewController实例,则配置文件视图也将被释放(假设没有其他人引用它)。 For the same reason, the LoginViewController instance is not released because another object is holding a reference to it. 出于相同的原因, LoginViewController实例不会被释放,因为另一个对象持有对该实例的引用。 Say your views are presented in Window -> ProfileViewController -> ProfileViewController , it's the window that keeps ProfileViewController instance from being released. 说你的意见中提出Window - > ProfileViewController - > ProfileViewController ,那就是保持ProfileViewController情况下被释放的窗口。

If you have two views as I assumed so far, the memory warning should be from somewhere else. 如果您到目前为止有两个假设,则内存警告应该来自其他地方。 Two views cannot cause the issue. 有两种视图不能引起问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM