ios - 无法呈现/推送另一个 View Controller

标签 ios swift

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/

相关文章:

ios - 旋转 MKMapView

objective-c - int 属性出现无法识别的选择器

ios - 动态更改文本输出 - React native

ios - 来自另一个类的值

php - 将音频从 Swift 应用程序发送到 PHP 服务器,但音频在某处丢失

swift - 保存sknodes数组

ios - 无法使用类型的参数列表调用 'getObjectInBackgroundWithId'

ios - 快速更改图像选择器首选状态栏样式

ios - Xcode 7 编辑时非常卡顿

ios - 在 Swift 中从 AVCaptureSession 捕获静止图像