我有一个 Root View Controller ,它有一个按钮,当用户按下它时,会显示另一个 View Controller 。
第二个 Controller 有一个关闭选项,它只返回 Root View Controller ,还有一个按钮,当用户触摸它时,它会关闭当前 View Controller ,因此它会返回 Root View Controller 一秒钟并显示另一个 View Controller 。
转到我使用的第一个 Controller :
let vc = FirstController()
self.present(vc, animated: true, completion: nil)
当我在另一个 View Controller 中选择只关闭的按钮时,我会这样做。
self.dismiss(animated: true, completion: nil)
因此对于需要关闭并显示另一个 Controller 的第二个 Controller ,我尝试了以下操作:
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)
})
这个问题和我answered的一个问题非常相似昨天。
针对您的情况进行了修改,我建议这样做:
weak var pvc = self.presentingViewController
self.dismiss(animated: true, completion: {
let vc = SecondController()
pvc?.present(vc, animated: true, completion: nil)
})
关于ios - 呈现一个 View Controller ,关闭它并在 Swift 中呈现一个不同的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43566414/