【问题标题】:Cannot present/push another viewcontroller无法呈现/推送另一个视图控制器
【发布时间】:2019-01-03 13:27:30
【问题描述】:

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

我有以编程方式创建的表格视图和情节提要中的其他视图,我试图从表格视图中选择一行来呈现。

类: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)
    }
}

【问题讨论】:

标签: ios swift


【解决方案1】:

你需要

 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:您可以使用 push 和删除当前的 vc(当前应该嵌入到导航中)

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

而不是present

【讨论】:

  • 我可以在 newviewload 之后关闭视图吗?
猜你喜欢
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 2019-03-28
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
相关资源
最近更新 更多