How to pass data between view controller and Container view using callbacks in iOS Swift?

782 Views Asked by At

I have a container view in my viewController.swift file. Now i want to pass data between Container view class and viewController.swift class. All demos are available using delegate function. But i want to perform this using callbacks. How i can achieve this?

1

There are 1 best solutions below

4
Daniel T-D On

The following site is a great reference for closures; http://fuckingclosuresyntax.com/

In your UIView declare a property to hold your closure;

var callback: ((CustomData) -> Void)?

Again in your UIView when you want to pass some data through that callback you can use;

callback?(someData)

Finally in your UIViewController you set your UIView's callback;

view.callback = { (data) in
    // perform some operations
}