目前一个控制器忽略它,并在Swift中呈现不同的一个

问题描述:

所以我有一个根视图控制器,当用户按下另一个视图控制器时,会有一个按钮。这第二个控制器有一个解除选项,它只是回到根视图控制器和一个按钮,当用户触摸它时会取消当前的视图控制器,以便它返回到根视图控制器一秒钟,并呈现另一个视图控制器。去我使用的第一个控制器:目前一个控制器忽略它,并在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! 

错误发生,因为您在解散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) 
})