Passing data through views by closures in Swift UI

Photo by fabio on Unsplash

When working with Swift UI, you are likely going to need to pass some data in between your views that may be being stored or used within another view. This process can actually be achieved pretty easily by passing the data through a closure statement with calling the next view. We are going to dive into this subject today so that you are able to easily pass data from one view to the next!

Closure Statements:

When passing data through closure statements, it is important that the view receiving the data is setup to actually utilize that data and accept the values that are being passed to it. There are two options you can choose to utilize when setting up your receiving view. You can either set the value as an explicit or an optional. By casting the variable as an explicit value, your view will require that value to not return nil otherwise your application will crash. When casting the variable as an optional however, your view may not require the value to run appropriately. You can also set a fallback statement in case the value you are passing is in fact nil. We will take a look at the syntax for closure statements below:

struct FirstView: View {
@State var name = ""

var body: some View {
NavigationView {
VStack {
Text("Enter your name below:")
TextField("name...", text: $name)
NavigationLink(destination: SomeView(name: name)) {
Text("Next page!")
}
}
}
}
}
struct SecondView: View {
var name: String!
var body: some View {
VStack {
Text(name)
}
}
}

If you look at this example above, we have our first view and our second view. The first view has a state variable called “name” that is empty but takes the users input in the TextField and stores that value as a name. Now when examining the second view, we have a variable called “name” as well but this variable currently does not have a value. Instead we have declared that this variable is of type String and marked it as explicit. If we called this view in our navigation link without passing in a value for the variable “name”, our application would crash. However, since we have a value that was input by the user that we are passing through, our application runs successfully.

When we are calling our second view, you can see that we are utilizing a closure statement. This is required in Swift UI when you are calling another view that has variables that have no value, only a set type. We utilize the syntax of the view name, SecondView, and then in our parenthesis we place the closure statement of the variable in the view needing a value, name, and then the value we are passing to it. In this case we are passing the name variable from our first view into the name variable of our second view.

Passing data with closures is as simple as that! Set up your variables in the receiving view to receive the data that we will pass in through the closure statement when calling the view. Then ensure that you are utilizing the correct type of variable, either explicit or optional. Finally utilize the closure statement when calling the view to pass the data into the new view.

You may also like these articles related to Swift UI:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Austin Beck

Austin Beck

var title = “iOS Engineer @ Lunchbox”