【发布时间】: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