reload tableView from Another ViewController Swift 4.2

眉间皱痕 提交于 2019-12-11 09:55:40

问题


when i am trying to call method from another VC reloadData() then the app crash Fatal error: Unexpectedly found nil while unwrapping an Optional value due to tablview nil how can resolve it.

FavoritesFiltersViewController.shareInstance.reloadData()


回答1:


  • You can use notificationCenter for the reload tableView data from
    another viewController like below :

1. You can add this line to another viewController.

 NotificationCenter.default.post(name: NSNotification.Name(rawValue: "newDataNotif"), object: nil)

2. Add this line(viewDidLoad) to viewController where contain tableView.

NotificationCenter.default.addObserver(self, selector: #selector(self.refresh), name: NSNotification.Name(rawValue: "newDataNotif"), object: nil)

3. Add this selector method for reload tableView data.

 @objc func refresh() {

    self.tblview.reloadData() // a refresh the tableView.

}

This is working fine. Thank you.




回答2:


Check the init function that's triggered when you call FavoritesFiltersViewController.shareInstance It seems that you are not giving a value to the tableView property.

Just to check, you can remove the optional from it and see if the compiler complains because it has no initial value.




回答3:


Thanks for every one.Finally after a R&D. i found the following solution. and its working fine.

let fav:FavoritesFiltersViewController! 
 fav.reloadData()


来源:https://stackoverflow.com/questions/53872958/reload-tableview-from-another-viewcontroller-swift-4-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!