How can you reload a ViewController after dismissing a modally presented view controller in Swift?

前端 未结 3 1765
既然无缘
既然无缘 2021-02-02 17:25

I have a first tableViewController which opens up a second tableViewcontroller upon clicking a cell. The second view controller is presented modally (Show Detail segue) and is d

3条回答
  •  一个人的身影
    2021-02-02 17:51

    Swift 5:

    You can access the presenting ViewController (presentingViewController) property and use it to reload the table view when the view will disappear.

    class: FirstViewController {
        var tableView: UITableView
    
        present(SecondViewController(), animated: true, completion: nil)
    }
    

    In your second view controller, you can in the viewWillDisappear method, add the following code:

    class SecondViewController {
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
    
            if let firstVC = presentingViewController as? FirstViewController {
                DispatchQueue.main.async {
                    firstVC.tableView.reloadData()
                }
            }
        }
    }
    

    When you dismiss the SecondViewController, the tableview of the FirstViewController will reload.

提交回复
热议问题