【问题标题】:Load a view controller and then remove the previous one from memory加载一个视图控制器,然后从内存中删除前一个
【发布时间】:2016-04-14 10:19:16
【问题描述】:

假设我有一个视图控制器 A(UIViewController 的子类),它有一个按钮,如果您按下该按钮,它将使用 segue 加载并显示视图控制器 B(UIViewController 的子类)(所以我使用界面生成器)。 Segue 类型为 Show。

当B出现时,我再也不会回到A了,所以我想杀死、摧毁、粉碎、残废它,好让宝贵的几个字节内存可以用于其他事情。

这是A的代码:

class ViewControllerA: UIViewController {
  deinit {
    print("I am immortal haha I cannot die!!!")
    print("This will not be displayed!!!")
  }
}

这是B的代码:

class ViewControllerB: UIViewController {
  override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    print("Go away old view controller!!!")
    appDelegate.window!.rootViewController = self // (1)
  }
}

我在 (1) 处的代码替换了应用程序的根视图控制器。但是,旧的视图控制器似乎在内存中徘徊,因为它的 deinit 的打印消息没有显示出来。

如何完成卸载不需要的视图控制器及其视图的简单任务?非常感谢

【问题讨论】:

    标签: ios objective-c iphone swift cocoa-touch


    【解决方案1】:

    当您从ViewControllerA 呈现ViewControllerB 时,ViewControllerB 在其presentingViewController 变量中持有ViewControllerA 的强引用,这就是为什么ViewControllerA 永远不会被取消初始化的原因。您需要做的是直接从ViewControllerA 更改应用程序代理窗口rootViewController,这意味着您必须摆脱segues。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多