在iOS 6中,viewWillUnload
和viewDidUnload
已棄用,UIViewController不再卸載在內存警告期間屏幕上不可見的視圖。 View Controller Programming Guide有一個如何手動恢復此行爲的示例。在內存警告(Apple文檔缺陷)中卸載iOS 6視圖的正確方法是什麼?
下面是代碼示例:
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Add code to clean up any of your own resources that are no longer necessary.
if ([self.view window] == nil)
{
// Add code to preserve data stored in the views that might be
// needed later.
// Add code to clean up other strong references to the view in
// the view hierarchy.
self.view = nil;
}
}
下面的代碼示例如下注意事項:
視圖屬性在下一次訪問,認爲是重載 正是因爲它是第一次。
這裏有一個明顯的缺陷。如果未加載其視圖的視圖控制器收到內存警告,則會將其視圖加載到線if ([self.view window] == nil)
中,然後繼續清理並再次釋放它。充其量,這是效率低下的。最糟糕的是,如果加載複雜的視圖層次結構和支持數據,則會使內存條件變差。我在iOS模擬器中驗證了這一行爲。
我當然可以對此進行編碼,但對於Apple文檔有這樣的錯誤似乎很奇怪。我錯過了什麼嗎?
爲了向後兼容,有'viewDidUnload'和'viewWillUnload'嗎?如果是這樣,我會建議評論代碼,說這兩個方法根本不會在iOS 6中調用(如[UIViewController docs](http://developer.apple.com/library/ios/#documentation /uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixAdeprecatedAPI.html#//apple_ref/occ/instm/UIViewController/viewWillUnload)state)。 – 2012-10-10 13:43:02
好的建議,thx。完成。 – XJones
如果你可以在' - didReceiveMemoryWarning'中相應地調用'[self viewWillUnload]'和'[self viewDidUnload]',而不是自定義' - my_view {Will | Did} Unload',它會不會更好? – Ali