Shahbaz Akram
Shahbaz Akram

Reputation: 1657

reload tableView from Another ViewController Swift 4.2

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()

enter image description here

Upvotes: 2

Views: 4541

Answers (3)

Shahbaz Akram
Shahbaz Akram

Reputation: 1657

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

let fav:FavoritesFiltersViewController! 
 fav.reloadData()

Upvotes: 1

Virani Vivek
Virani Vivek

Reputation: 896

  • 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.

Upvotes: 12

FedeH
FedeH

Reputation: 1363

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.

Upvotes: 1

Related Questions