Search code examples
swiftobservablerx-swift

Observe value change from Cell to ViewController Swift


I'm in trouble because I have a TableView with inside a CollectionView and I don't know how I can pass the current data that are inside my CollectionViewCell once the user clicked on it (didSelectItemAt).

So I would wish to know if there is a way for observe the value that changed inside a variable.

For example. When user is clicking the cell, a variable is updated with the current data contained in that cell and then I'm receiving the update in my ViewController that once that has been observed the value change, will call a function for update some values in it.

Thanks!


Solution

  • I found out that the best way for hear changes is to use NotificationCenter:

    NotificationCenter.default.post(name: Notification.Name("aNotificationName"), object: nil)
    

    Then from my Controller:

    NotificationCenter.default.addObserver(self, selector: #selector(handleNotificationFuncName, name: NSNotification.Name(rawValue: "aNotificationName"), object: nil)