I have a TableViewController, lets call it A, that is in a container view of another view controller, B. I need A to reload it's data when a value changes in B. I also need it to get this changed value from B. Any ideas?
Have you considered using notifications?
So, in B – I would do something like:
// ViewControllerB.swift
import UIKit
static let BChangedNotification = "ViewControllerBChanged"
class ViewControllerB: UIViewController {
//... truncated
func valueChanged(sender: AnyObject) {
let changedValue = ...
NSNotificationCenter.defaultCenter().postNotificationName(
BChangedNotification, object: changedValue)
}
//... truncated
}
Followed up with A looking something like this – where ValueType
is simply the type of the value you mentioned:
import UIKit
class ViewControllerA: UITableViewController {
//... truncated
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//...truncated
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "onBChangedNotification:",
name: BChangedNotification,
object: nil)
}
//... truncated
func onBChangedNotification(notification: NSNotification) {
if let newValue = notification.object as? ValueType {
//...truncated (do something with newValue)
self.reloadData()
}
}
}
Lastly – don't forget to remove the observer in A's deinit
method:
import UIKit
class ViewControllerA: UITableViewController {
//... truncated
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
//... truncated
}