2017-04-23 51 views
0

所以我有一個根視圖控制器,當用戶按下另一個視圖控制器時,會有一個按鈕。這第二個控制器有一個解除選項,它只是回到根視圖控制器和一個按鈕,當用戶觸摸它時會取消當前的視圖控制器,以便它返回到根視圖控制器一秒鐘,並呈現另一個視圖控制器。去我使用的第一個控制器:目前一個控制器忽略它,並在Swift中呈現不同的一個

let vc = FirstController() 
self.present(vc, animated: true, completion: nil) 

而當在另一個視圖控制器我選擇按鈕,只有解僱我這樣做。

self.dismiss(animated: true, completion: nil) 

因此,對於需要解僱並提出另外一個我曾嘗試以下第二個控制器:

self.dismiss(animated: true, completion: { 
      let vc = SecondController() 
      self.present(vc, animated: true, completion: nil) 
     }) 

但我得到一個錯誤:

Warning: Attempt to present <UINavigationController: 0xa40c790> on <IIViewDeckController: 0xa843000> whose view is not in the window hierarchy! 

回答

5

錯誤發生,因爲您在解散FirstController後嘗試從FirstController呈現SecondController。這是行不通的:

self.dismiss(animated: true, completion: { 
    let vc = SecondController() 

    // 'self' refers to FirstController, but you have just dismissed 
    // FirstController! It's no longer in the view hierarchy! 
    self.present(vc, animated: true, completion: nil) 
}) 

此問題與昨天的問題I answered非常相似。

修改您的方案,我建議這樣的:

weak var pvc = self.presentingViewController 

self.dismiss(animated: true, completion: { 
    let vc = SecondController() 
    pvc?.present(vc, animated: true, completion: nil) 
}) 
相關問題