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?
Asked
Active
Viewed 700 times
1 Answers
1
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
}

Daniel T-D
- 665
- 6
- 8
-
you can replace `if..let` with `callback?(someData)` – Desdenova Dec 13 '18 at 11:49
-
Ah of course, I'd just quickly typed it up in Playground, I'll update my answer now. Thanks. – Daniel T-D Dec 13 '18 at 11:51
-
Yes, but i am talking about data from container view to viewcontroller. Container view is kind of UIViewController not UIVIew. – Manan Shah Dec 13 '18 at 11:55
-
1The file type shouldn't make a difference? If you can get a reference to either, allowing you to set up a delegate then you can get a reference to either to set a closure. – Daniel T-D Dec 13 '18 at 12:00