今天發(fā)現(xiàn) APP 中有幾個包含手寫控件的 ViewController 不會自動釋放內(nèi)存卖子,即不會主動調(diào)用 deinit琼了。通過在 ViewDidDisappear 中 添加 self.view = nil 可以觸發(fā)調(diào)用 deinit 。但是無法區(qū)分是結(jié)束 ViewController 或者是進入后臺。在 OC 時代可以使用下面代碼來判斷是否已經(jīng)關(guān)閉視圖。
if (![[self.navigationController viewControllers] containsObject: self])
{
// the view has been removed from the navigation stack, back is probably the cause
// this will be slow with a large stack however.
}
在 Swift 中不能用 containsObject 了,最后發(fā)現(xiàn)直接判斷 self.navigationController 即可達到同樣的目的铸豁。
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
if let nc = self.navigationController{
// 在后臺
}else{
// 已關(guān)閉
// 觸發(fā) deinit
self.view = nil
}
}
參考:http://blog.csdn.net/xiaochong2154/article/details/45603511