2016-01-21 43 views
0

所以我的應用程序做的第一件事就是從API獲取電影列表。我試圖處理,如果有網絡問題。目前,在我viewDidLoad方法,我稱之爲 「updateApiInfo」,它包含以下代碼:iOS:在用戶看到視圖後需要執行某些操作時,應覆蓋哪個函數?

if self.movies == nil { 
      print("stuff went wrong") 
      let alert = UIAlertController(title: "Network Error", message: "There was a nework error.\nYou must be connected to the internet to use Flicks.", preferredStyle: UIAlertControllerStyle.Alert) 
      alert.addAction(UIAlertAction(title: "Exit", style: UIAlertActionStyle.Default, handler: {(UIAlertAction) in 
       UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil) 
      })) 
      self.presentViewController(alert, animated: true, completion: nil) 
} 

當viewDidLoad中調用updateApiInfo,我得到這個消息:

Warning: Attempt to present <UIAlertController: 0x7fad10e3ad80> on <Flicks.MoviesViewController: 0x7fad10e35cc0> whose view is not in the window hierarchy! 

當我打電話updateApiInfo剛剛從用戶刷新,錯誤按預期彈出。

我假設viewDidLoad只在之前被調用視圖顯示給用戶,我猜是造成這個錯誤。有沒有一種方法可以將這個代碼粘貼到之後,這個視圖就會顯示給用戶,所以這個問題可以想象得到修復嗎?

+0

沒有,'當視圖被實例化viewDidLoad'被調用,但呈現前。實際顯示視圖後,您通常會將'viewDidAppear'用於東西。 – Rob

+0

沒錯。 'viewDidLoad()'只保證視圖控制器的主視圖被實例化(以及在Interface Builder中定義的任何子視圖)。 –

+0

@CaptainForge - 我的答案在下面回答你的問題? – siburb

回答

相關問題