Closure
Closure is an important part of the swift language, it has the various use & implementation in swift, here I will provide the one use where you can use to communicate between two-controller.
There is a scenario where we will perform some operation on 2nd controller & get their data in the previous controller for this we can use protocol for this, but we can use closure for this
and reduced the unwanted code & improve the readability.
For more usability & implementation you can read this:
https://docs.swift.org/swift-book/LanguageGuide/Closures.html
Let’s start to do this
ViewController1 -> ViewController2 .
Notes: Here we have used 2 controllers, the user will go from viewcontroller1 to viewcontroller2 here it will perform some operation & get back with data to viewcontroller1
1: Write this code on viewcontroller2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
class PinAddressController: UIViewController { var locationData:LocationData! var getMapData : ((_ data:LocationData)-> Void)? // Here we have taken the closure whose parameter is LocationData & it will not return value. override func viewDidLoad() { super.viewDidLoad() self.locationData = LocationData() // You can set your data or perform some operation then update the "self.locationData". } @IBAction func doneClick(_ sender: UIButton) { self.dismiss(animated: true, completion: nil) getMapData?(locationData) } } class LocationData{ var country:String = "" var countryCode:String = "" var state:String = "" var zip:String = "" var street:String = "" var city:String = "" var lat:Double = 0.0 var lon:Double = 0.0 init() { } init(data:[String:String],lat:Double,lon:Double) { self.country = data["country"] ?? "" self.countryCode = data["countryCode"] ?? "" self.state = data["state"] ?? "" self.zip = data["zip"] ?? "" self.street = data["street"] ?? "" self.city = data["city"] ?? "" self.lat = lat self.lon = lon } } |
Here, actually we need some data from “PinAddressController” that will return through “LocationData” Object so we just return this data to caller controller as input param in a Closure.
When the user will click on the done button then we will dismiss the controller or you can pop back to the previous controller after that return the data.
2:Now write the first controller code means (viewcontroller1)
This controller will present the viewcontroller2 or above the controller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class AddressSuggestionController: UIViewController{ override func viewDidLoad() { super.viewDidLoad() } @IBAction func getDatataFromOtherController(_ sender: UITapGestureRecognizer) { let viewController = PinAddressController.instantiate(fromAppStoryboard: .hyperlocal) // Initiate your desination controller through Storyboard. viewController.getMapData = {[weak self] userData in print(userData) // Here you will get the actual data } let navController = UINavigationController(rootViewController: viewController) self.present(navController, animated: true) } } |
Notes:
When you will click on getDatataFromOtherController button then it will present the destination controller & after done the second controller which is already on above you will
get the data in “userData”