2012-10-10 30 views
1

viewDidUnload在iOS6的不再是所謂的,所以作爲一個應用程序,並在viewDidUnload一些必要的事情解決方法我已經做到了這一點:將蘋果拒絕的應用程序進行手動調用viewDidUnload

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 


    // only want to do this on iOS 6 
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0) { 
     // Don't want to rehydrate the view if it's already unloaded 
     BOOL isLoaded = [self isViewLoaded]; 

     // We check the window property to make sure that the view is not visible 
     if (isLoaded && self.view.window == nil) { 

      // Give a chance to implementors to get model data from their views 
      [self performSelectorOnMainThread:@selector(viewWillUnload) 
            withObject:nil 
           waitUntilDone:YES]; 

      // Detach it from its parent (in cases of view controller containment) 
      [self.view removeFromSuperview]; 
      self.view = nil; // Clear out the view. Goodbye! 

      // The view is now unloaded...now call viewDidUnload 
      [self performSelectorOnMainThread:@selector(viewDidUnload) 
            withObject:nil 
           waitUntilDone:YES]; 
     } 
    } 
} 

是否有任何先例蘋果拒絕這樣的事情?由於時間限制,我不能冒險拒絕任何東西。

回答

0

他們沒有理由拒絕這個。通過摒棄該方法,-[UIView viewDidUnload]就像其他方法一樣。你可以把它看作是從來沒有在UIView首先存在,你恰好創建了一個叫做-viewDidUnload的方法。

是一個問題,如果-viewDidUnload仍然存在內部(它可能),你試圖調用蘋果公司(現爲私人)實現,而不是你自己的,但我很懷疑蘋果會做到這一點。不過,請務必-viewDidUnload記得要問,如果超類試圖調用它超級,如果這就是你現在在做什麼,使用前實現方法:

if ([[self superclass] instancesRespondToSelector:@selector(viewDidUnload)]) { 
    [super viewDidUnload]; 
} 

如果你真的想成爲安全你可以始終將您的代碼移至其他方法。裏面viewDidUnload只是叫你的新方法,爲iOS 5設備和-didReceiveMemoryWarning打電話給你的新方法,如果你在iOS 6

我不會在你的didReceiveMemoryWarning的邏輯在長度上發表評論,因爲這是不在問題中,但我會說你應該非常小心你將控制器放在哪個狀態(確保if語句覆蓋了所有的基礎!)當然,你不能指望你的視圖控制器和它的視圖當你調用viewDidUnload的時候,它會在iOS 5中被UIKit調用時處於相同的狀態。

1

Apple沒有理由拒絕它,但是他們因爲某種原因刪除了它。

手動調用它只是要求問題。我強烈建議正確地使用應用程序邏輯,並採用「正確」的方式。

編輯:再次查看該代碼後,我對您的實現有嚴重懷疑。帶有調用選擇器的整個構造(我們應該已經在主線程上了!)並從超級視圖中移除(它只是在不告訴它的情況下從它的所有者中竊取視圖)就是不正確的。這是您真正想要從您的代碼庫中消除的代碼類型。

相關問題