0

Warning: Attempt to present iChat.ChatMessagesController: 0x7fee42679fd0> on iChat.NewMessageController: 0x7fee42529e60> whose view is not in the window hierarchy!

i have tableview that is created programmatically and other view in storyboard that iam trying to present from selected a row from the table view.

Class : ChatMessagesController

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)
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
SemarY
  • 49
  • 1
  • 9
  • Possible duplicate of [Dismiss and Present View Controller in Swift](https://stackoverflow.com/questions/37771001/dismiss-and-present-view-controller-in-swift) – Rakesha Shastri Jan 03 '19 at 13:29

1 Answers1

2

You need

 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)

}

as this dismiss(animated: true) { will dismiss the current vc , and you'll not be able to present anything inside it , if you need to completely remove the current vc and replace with the new one , then do

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

EdiT: You can use push and remove the current vc with ( current should be embedded in a navigation )

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

instead of present

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87