How to reload tableview from another view controller in swift

前端 未结 6 1893
后悔当初
后悔当初 2020-11-28 22:40

When I dismiss a modal view controller I want the tableview to update, I am using the form sheet presentation style on iPad so the viewWillAppear and the

6条回答
  •  抹茶落季
    2020-11-28 23:01

    An alternate solution: override UIViewController's dismiss(animated:completion:) method on the view controller that manages the table view (and presents the other one modally), so you can reload the table then:

    override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
        super.dismiss(animated: flag, completion: completion)
    
        self.tableView.reloadData()    
    }
    

    Note: This should work even if you call dismiss(animated:completion:) on the modal view controller itself (a viable alternative), because ultimately the call gets relayed to the presenting view controller.

    From the method's docs:

    The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.

    (emphasis mine)

提交回复
热议问题