问题
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