swift - What is the best way to reload a child tableview contained within a parent VC that is being fed fresh data? -


i have been playing concept of parent/child view delegation few days now, , understand how feed data parent child. however, now, want button in parent (main vc) reload data presented in child vc.

i'm trying delegate method activated in child vc's class activated in parent's navigation controller. when press button, delegated method in child vc performed; in case, method reload table. why getting many errors when trying set simple delegation relationship?

my parent/container view delegating method child, have set child -> parent. want set parent -> child. pretty have:

struct constants {     static let embedsegue = "containertocollectionview" }   class containerviewcontroller: uiviewcontroller, collectionviewcontrollerdelegate {  func givemedata(collectionviewcontroller: collectionviewcontroller) {     println("this data passed") }  override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {     if segue.identifier == constants.embedsegue {         let childviewcontroller = segue.destinationviewcontroller as! collectionviewcontroller         childviewcontroller.delegate = self     } } 

from child:

protocol collectionviewcontrollerdelegate {     func givemedata(collectionviewcontroller: collectionviewcontroller) }  class collectionviewcontroller: uiviewcontroller {  var delegate:collectionviewcontrollerdelegate?  override func viewdidload() {     super.viewdidload()       self.delegate?.givemedata(self)      // additional setup after loading view. } 

i think trouble fact i'm declaring child delegate in prepareforsegue, straight forward, want reverse delegation. how set can use child-method parent vc?

the child view controller has no business supplying other controllers data. should not have data fetching logic generic used other controllers. should refactor data methods out new class.

this pattern called model-view-controller, or mvc, , basic concept should understand , follow. apple explains pretty well.

in general, send data controller detail controller, use prepareforsegue set properties, etc. communicate parent controller, use delegate protocols, these called when detail controller finished work , reports result parent.

if want update detail vc new data (without dismissing , parent not visible) should not put logic update parent. instead, use structure suggested above.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -