I’m asking for advise on the code below. Note this part…
’ List($theAddresses, id: .self) { addr in
TextField(“Address”, text: addr)
}
HStack {
TextField(“City”, text: $city)
TextField(“State”, text: $state)
TextField(“Zip Code”, text: $zipPostal)
}
’
Using the simulator, I can put the cursor into the “Address” field, but after a keystroke, the field looses focus and I have to click in the field again. I looked at several solutions, but they all seemed overly complicated. The complete view code is below…
’
//
// AddressView.swift
// Secure Address Book
//
// Created by Jim Olivi on 1/19/22.
//
import SwiftUI
struct AddressView: View {
var oldAddress: Address
@Environment(\.presentationMode) var doDismiss
@State private var theAddresses: [String] = [String]()
@State private var city: String = ""
@State private var state: String = ""
@State private var zipPostal: String = ""
@State private var statusMessage: String = ""
var body: some View {
GeometryReader { geo in
//MARK: Enter view
Form {
Section(header: HStack {
Button(action: setData) {
Text("Cancel")}
Spacer()
Button(action: saveData) {
Text("Save")}
Spacer()
Button(action: { deleteAddress(oldAddress: oldAddress)
doDismiss.wrappedValue.dismiss()
}) {Text("Delete")}
}, footer: VStack {
Text(statusMessage)
.font(.headline)
.fontWeight(.bold)
}, content: {
List($theAddresses, id: \.self) { addr in
TextField("Address", text: addr)
}
HStack {
TextField("City", text: $city)
TextField("State", text: $state)
TextField("Zip Code", text: $zipPostal)
}
})
}
.frame(width: geo.size.width, height: geo.size.height * 0.6, alignment: .center)
.navigationTitle("Address")
.environmentObject(Address())
.onAppear{setData()}
.onDisappear {flushAddressCoreData()}
}
}
func setData() {
// Restore the old properties
theAddresses = oldAddress.address!
city = oldAddress.city ?? ""
state = oldAddress.state ?? ""
zipPostal = oldAddress.zipPostalCode ?? ""
statusMessage = "\(oldAddress.address![0]) information displayed."
}
func saveData() {
oldAddress.address! = theAddresses
oldAddress.city = city
oldAddress.state = state
oldAddress.zipPostalCode = zipPostal
statusMessage = "Address Saved for \(theAddresses[0])"
}
//struct AddressView_Previews: PreviewProvider {
// static var previews: some View {
//
// var a = Address()
//
// a.address = "125 Veterans Way"
// a.city = "Carmel"
// a.state = "IN"
// a.zipPostalCode = "46032"
//
// AddressView(oldAddress: a)
// }
//}
}
’