This is more of a general understanding question regarding SwiftUI.
What I would like to be able to do is to execute a function associated with another view. There are a number of cases/reasons for doing this. My most recent endeavor is to have a view that collects a number of fields, like an address, then to use that view within other views.
My question is, how do I get the data from the address view? In the example shown, how might I call the getXML() function? Is there a better way to accomplish this?
import SwiftUI
struct AddressView: View
{
@State var street1 = ""
@State var street2 = ""
@State var city = ""
@State var state = ""
@State var zip = ""
var body: some View
{
VStack
{
TextField("Street1", text: $street1)
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("Street2", text: $street2)
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("City", text: $city)
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("State", text: $state)
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("Zip", text: $zip)
.textFieldStyle(RoundedBorderTextFieldStyle())
}.padding()
}
func getXML() -> String
{
var xml = XMLStuff("Address")
xml.push(tag: "street1", value: street1)
xml.push(tag: "street2", value: street2)
xml.push(tag: "city", value: city)
xml.push(tag: "state", value: state)
xml.push(tag: "zip", value: zip)
return xml.string()
}
}
The following is some code that would be used to include several such views but I can't figure out how to extract the data in order to save/use it.
import SwiftUI
struct ContactView: View
{
@State var name: String = ""
var body: some View
{
VStack
{
TextField("Name", text: $name)
AddressView()
PhoneView()
}
}
}
I know I could create a bunch of state variables in the caller and then pass them to the sub-view, but I'm trying to avoid that if possible.
getXMLcan be placed in that view model class.