无法显示/推送另一个ViewController

时间:2019-01-03 13:27:31

标签: ios swift

  

警告:尝试在其视图不在窗口层次结构中的iChat.NewMessageController:0x7fee42529e60>上显示iChat.ChatMessagesController:0x7fee42679fd0>!

我有以编程方式创建的表视图,而故事板中的其他视图是我试图从表视图的选定行中呈现的视图。

Class:ChatMessagesController

故事板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)
    }
}

1 个答案:

答案 0 :(得分:2)

您需要

 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并替换为新的vc,请执行

(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController  = newViewController

EdiT :您可以使用push和删除当前的vc,(当前应嵌入到导航中)

self.navigationController?.pushViewController([newViewController], animated: true)

代替present