首页 > 解决方案 > UITabBarController 和解雇

问题描述

在每个 UIViewController 中,我得到了带有简单打印语句的 deinit 函数,该语句告诉我该特定 UIViewController 何时从内存中删除。

deinit {
    print("VIEWCONTROLLER DELETED FROM MEMORY")
}

但是,作为 UITabBarController 一部分的所有 UIViewController 永远不会调用 deinit ,我也永远不会在控制台中看到打印语句。

除此之外:

里面的代码...

NotificationCenter.default.addObserver(forName: UIApplication.didChangeStatusBarOrientationNotification, object: nil, queue: OperationQueue.main) { (notification) in


           print("Device rotated")
        }

...也将在其他选项卡上调用。因此,当设备从横向切换到纵向时,即使我不在特定代码所在的 UIViewcontroller 中,上述闭包内的所有代码也将被执行……奇怪!

这是为什么?

标签: memoryuiviewcontrolleruitabbarcontrollerdismiss

解决方案


好吧,UITabBarController 正在按预期完美运行。可能是你走错了路。如果要观察通知,可以在 UITabBarController 类中观察它,而不是在 UITabBarController 内部的 ViewController 中观察它。有关更多信息,您可以浏览UITabBarController的苹果文档。


推荐阅读