Communicate through Closure between two controller using swift 4.

Updated 24 November 2019

Save

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

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.

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

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home