38

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 viewDidAppear methods will not work

Suneet Tipirneni
  • 809
  • 1
  • 12
  • 17

6 Answers6

90

You can do this:

In your tableView Controller:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
}
 
@objc func loadList(notification: NSNotification){
    //load data here
    self.tableView.reloadData()
}

Then in the other ViewController :

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
Codetard
  • 2,026
  • 22
  • 33
Sebastian
  • 5,137
  • 5
  • 29
  • 47
  • 1
    This isn't working for me for some reason. I added a breakpoint at the line `//load data here` but the table view doesn't show the new data added – Zack Shapiro Mar 08 '15 at 22:39
  • Thanks, this perfect solution, I was looking for some thing like this. – Hussain KMR Behestee Nov 15 '15 at 07:21
  • Worked for Me, I even called the `postNotificationName` function from a separate class, not even a viewcontroller – jeffery_the_wind Apr 04 '16 at 14:17
  • @ZackShapiro try to set that dispatch_async(dispatch_get_main_queue(), { self.tableView.reloadData() }); rather than self.tableView.reloadData() , it worked for me – Hosny Apr 24 '17 at 15:19
  • This answer should be marked, it works, it's clean and understandable. – Emm Sep 13 '18 at 20:05
  • Perfectly!! I'm searching a way to reload like 3 tableView's data in 3 VC from other VC. Now I could sync the data in which is not current active VC. Thanks, bro. – ChuckZHB Jan 14 '20 at 13:05
49

Swift 3 version code: In your first view controller:

override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
    }

func loadList(){
        //load data here
        self.tableView.reloadData()
    }

In your second view controller:

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
mriaz0011
  • 1,799
  • 20
  • 11
2

Missing comma on this line, should instead be:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:", name:"load", object: nil)
Rafael
  • 5,613
  • 4
  • 36
  • 44
kauai
  • 469
  • 1
  • 6
  • 9
2

I find the segue approach more elegant.

Let's say that we have ViewControllerA and a ViewControllerB. We are at ViewControllerB and we want from ViewControllerB to jump straight back to ViewControllerA and update the table view in ViewControllerA.

In ViewControllerA add the following action in your ViewController class:

@IBAction func unwindToViewControllerA(segue: UIStoryboardSegue) {
    DispatchQueue.global(qos: .userInitiated).async {
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }
}

Yes, this code goes in the ViewController of the ViewController you want to go back to!

Now, you need to create an exit segue from the ViewControllerB's storyboard (StoryboardB). Go ahead and open StoryboardB and select the storyboard. Hold CTRL down and drag to exit as follows:

enter image description here

You will be given a list of segues to choose from including the one we just created:

enter image description here

You should now have a segue, click on it:

enter image description here

Go in the inspector and set a unique id: enter image description here

In the ViewControllerB at the point where you want to dismiss and return back to ViewControllerA, do the following (with the id we set in the inspector previously):

self.performSegue(withIdentifier: "yourIdHere", sender: self)

Now, whenever you use the segue to return back to ViewControllerA, the ViewControllerA will update the TableView straightaway.

Rafael
  • 5,613
  • 4
  • 36
  • 44
1

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)

Nicolas Miari
  • 15,044
  • 6
  • 72
  • 173
0

You can use NotificationCenter to update your tableview.

First add observer...

NotificationCenter.default.addObserver(self, selector: #selector(doThisWhenNotify(notification:)), name: NSNotification.Name(rawValue: "load"), object: nil)

func doThisWhenNotify(notification : NSNotification) {
    let info = notificatio.userInfo
    //update tableview

}

Post on other ViewController

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil, userInfo: [String : Any])
Deep
  • 335
  • 4
  • 14