Warning: Attempt to present iChat.ChatMessagesController: 0x7fee42679fd0> on iChat.NewMessageController: 0x7fee42529e60> whose view is not in the window hierarchy!
我有以编程方式创建的 TableView 和 Storyboard中的其他 View ,我试图从 TableView 中选择的一行中呈现。
类:聊天消息 Controller
Storyboard ID ChatMessagesController
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
dismiss(animated: true) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "ChatMessagesController") as! ChatMessagesController
self.present(newViewController, animated: true, completion: nil)
}
}
最佳答案
你需要
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "ChatMessagesController") as! ChatMessagesController
self.present(newViewController, animated: true, completion: nil)
}
因为这个 dismiss(animated: true) {
将关闭当前的 vc ,并且如果您需要完全删除当前的 vc 并替换为,您将无法在其中显示任何内容新的,然后做
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = newViewController
EdiT:您可以使用推送和删除当前 vc(当前应嵌入导航中)
self.navigationController?.pushViewController([newViewController], animated: true)
而不是 present
关于ios - 无法呈现/推送另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54023226/