更改根视图控制器不会取消分配同一窗口上的先前视图控制器 [英] Changing the root view controller is not deallocating the previous view controllers on the same window

查看:23
本文介绍了更改根视图控制器不会取消分配同一窗口上的先前视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用启动时带有一个初始视图控制器(我们称之为 StartVC).现在,当用户按下继续按钮时,我会在 StartVC 顶部呈现一个导航堆栈(我们称之为 RegisterVC).这个导航堆栈将包含 5 个视图控制器,每当用户通过按钮操作向前移动时,我都会推动它.在第 5 个视图控制器之后,我开始了一个新的导航堆栈(我们称之为 LoginVC).

My app launches with an initial view controller (lets call it as StartVC). Now when user presses a continue button, I am presenting a navigation stack (lets call it as RegisterVC) on top of StartVC. This navigation stack will contain 5 view controllers which I am pushing on it whenever user moves forward with button actions. After the 5th view controller, I am starting a new navigation stack (lets call it as LoginVC).

现在我的用例是我不想要 StartVC &RegisterVC 驻留在内存中,因为一旦用户完成注册,它们就没有用了.为了实现这一点,我将 AppDelegate 窗口的根视图控制器更改为 LoginVC

Now my use case is I dont want the StartVC & RegisterVC to reside in the memory as they are of no use once user has completed his registration. In order to achieve this, I am changing the AppDelegate window's root view controller to LoginVC

以下是我在 RegisterVC 的第 5 个视图控制器上尝试的选项:

Below are the options which I tried on the 5th view controller of RegisterVC:

1) 改变键窗

    UIApplication.shared.keyWindow?.rootViewController = LoginVC
    UIApplication.shared.keyWindow?.makeKeyAndVisible()

2) 改变窗口

   let appDelegate = UIApplication.shared.delegate as! AppDelegate
   appDelegate.window?.rootViewController = LoginVC
   appDelegate.window?.makeKeyAndVisible()

3) 在分配新的根视图控制器之前,将之前的根视图控制器设为 nil.

3) Making the previous root view controller as nil before assigning a new one.

   let appDelegate = UIApplication.shared.delegate as! AppDelegate
   appDelegate.window?.rootViewController = nil
   appDelegate.window?.rootViewController = LoginVC
   appDelegate.window?.makeKeyAndVisible()

4) 我也直接从 AppDelegate 尝试了上述选项,而不是从第 5 个视图控制器中尝试.

4) I also tried the above options directly from the AppDelegate instead of doing it from the 5th view controller.

使用上述所有选项,我尝试通过查看所有视图控制器上的 deinit 进行调试,但没有一个被释放.另外,我可以在 xcode 调试视图层次结构中的 LoginVC 下看到第 5 个视图控制器.

With all the above options, I tried debugging by looking at deinit on all view controllers, but none of them got deallocated. Also, I can see that 5th view controller under LoginVC in the xcode Debug View Hierarchy.

因为没有从内存中删除它们,我面临的实际问题是在呈现 LoginVC 之后,我有一个背景颜色 alpha 较小的视图控制器.因此,我看到了它下面的 RegisterVC 第 5 个视图控制器.

Because of not removing them from memory, the actual problem which I am facing is after presenting the LoginVC, I have a view controller whose background color alpha is less. Because of this I am seeing the RegisterVC 5th view controller underneath it.

对此的任何帮助表示赞赏...

Any help on this appreciated...

推荐答案

我认为 rootViewController 设置不是问题.也许您在其他视图控制器中有一个保留周期,阻止它们被释放.

I think the rootViewController setting is not the problem. Perhaps you have a retain cycle in your other view controllers that stops them from being deallocated.

有很多方法可能会意外地做到这一点(在块中捕获对 self 的强引用,而不是将委托或其他反向引用标记为弱引用等).

There are many ways you could accidentally do this (capturing strong references to self in blocks, not marking delegates or other back references as weak, etc).

你也许可以用 Instruments 来解决这个问题.这是一个教程:http://samwize.com/2016/05/30/finding-retain-cycle-with-instruments/

You might be able to figure it out with Instruments. Here's a tutorial: http://samwize.com/2016/05/30/finding-retain-cycle-with-instruments/

这篇关于更改根视图控制器不会取消分配同一窗口上的先前视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆