MessagePeopleTableViewController:0x7fb47dd44f80>其视图不在窗口层次结构中

时间:2017-08-06 06:41:00

标签: ios swift

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    dismiss(animated: true) {
        let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatPage")
        self.present(viewController, animated: true, completion: nil)

        print("111")

    }
}

在解除一个视图控制器并通过completionblock移动到另一个视图控制器时出错
请帮我解决这个问题,我提到了网站,但我仍然被困在这里。

1 个答案:

答案 0 :(得分:1)

看起来,你要呈现" ChatPage"来自已被解雇的当前控制器的控制器。我认为这就是原因。

如果您当前的控制器是Modal,您可以尝试这样的事情:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            dismiss(animated: true) {

        let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatPage")     

        //get access to presenting parent controller
        self.presentingViewController.present(viewController, animated: true, completion: nil)

        print("111")

    }
}

或者在当前控制器中保存父控制器实例,但实际上这不是好的代码。

如果这不起作用,另一种方法是使用一个委托,它将调用父控制器中的某个方法来呈现某些东西。这意味着你应该从应该提供一个新控制器的控制器调用self.present(viewController,animated:true,completion:nil)(在你的情况下它是我猜的父控制器),但不是来自将被解除的控制器< / p>