39

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

2
  • 1
    You can use NSNotificationCenter to fire a method that update your tableview. Commented Sep 18, 2014 at 20:24
  • Can u show me how, im not very familiar with NSNotificationCenter thanks Commented Sep 18, 2014 at 20:28

6 Answers 6

100

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)
Sign up to request clarification or add additional context in comments.

6 Comments

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
Thanks, this perfect solution, I was looking for some thing like this.
Worked for Me, I even called the postNotificationName function from a separate class, not even a viewcontroller
@ZackShapiro try to set that dispatch_async(dispatch_get_main_queue(), { self.tableView.reloadData() }); rather than self.tableView.reloadData() , it worked for me
This answer should be marked, it works, it's clean and understandable.
|
50

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)

3 Comments

where in the second VC?
Anywhere in your second view controller where you think it's time to update the tableView possibilities are: methods, viewDidLoad or with in any part of code.. Hope its helpful
great, lifesaver solution!
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.

Comments

2

Missing comma on this line, should instead be:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:", name:"load", object: nil)

Comments

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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.