SwiftUI将选择器数据保存到阵列

时间:2020-10-02 12:43:46

标签: swiftui picker

我正在根据JSON显示城市信息,但是每当我键入另一个文本字段时,此选择器都会重置自身并返回到Adana。另外,我想将此城市添加到我的creditCard数组中,但无法弄清楚。

struct LocationView: View {
@State var selectedFrameworkIndex = 0
@State var selectedCityIndex = 0
@State var tap1 : Bool = false
@ObservedObject var cityfetch = CityFetcher()

var selectedCityName: String? {
    if !self.cityfetch.uniqueCities.isEmpty && selectedFrameworkIndex < self.cityfetch.uniqueCities.count {
        return self.cityfetch.uniqueCities[selectedFrameworkIndex].name
    } else {
        return nil
    }
}

var body: some View {
    HStack{
        Text("Il")
            .font(Font.custom("SFCompactDisplay", size: 16))
        Picker(selection: $selectedFrameworkIndex, label: Text("Seciniz")) {
            ForEach(0 ..< self.cityfetch.uniqueCities.count, id: \.self) {
                Text(self.cityfetch.uniqueCities[$0].name)
            }
        }
        .font(Font.custom("SFCompactDisplay", size: 10))
        .background(Color(.white))
        .foregroundColor(.gray)
        .frame(width: 150, height: 30)
        .clipped()
        .cornerRadius(5) 
    }
}

我在下面的PersonalAddressView中调用LocationView

    import SwiftUI
import Combine
struct PersonalAddressView: View {
//    @Binding var isShown: Bool
    @State var personalAddress: PersonalAddress
    @State var isSaved: Bool = false
    @State private(set) var addressNameText = ""
    @State private(set) var nameSurnameText = ""
    @State private(set) var cityText = ""
    @State private(set) var countyText = ""
    @State private(set) var districtText = ""
    @State private(set) var addressText = ""
    var body: some View {
        VStack {
            AddressTextField(data: $addressNameText, captionText: "Adres Adi")
                .onReceive(Just(addressNameText)) { addressNameData in
                    if addressNameText.count > 1 {
                        self.personalAddress.addressName = self.addressNameText
                    }
                }
            AddressTextField(data:$nameSurnameText, captionText: "Ad-Soyad")
                .onReceive(Just(nameSurnameText)) { nameSurnameData in
                    if nameSurnameText.count > 6 {
                        self.personalAddress.nameSurname = self.addressNameText
                    }
                }
            HStack {
                LocationView()
//                self.personalAddress.city = LocationView().selectedCityName! => trying to set this city name to my creditcard array similar to addressName or nameSurname

下面的这是我的PersonalAddress模型和PersonalAddressed数组

class PersonalAddress: Identifiable, Codable {
var id: String = UUID().uuidString
var addressName: String
var nameSurname: String
var city: String
var county: String
var district: String
var address: String

var items = [PersonalAddress]()

init(addressName: String, nameSurname: String, city: String, county: String, district: String, address: String) {
    self.addressName = addressName
    self.nameSurname = nameSurname
    self.city = city
    self.county = county
    self.district = district
    self.address = address
  }
}


class PersonalAddresses: ObservableObject {
@Published var items = [PersonalAddress]()

func appendNewPersonalAddress(personalAddress: PersonalAddress) {
    objectWillChange.send()
    items.append(PersonalAddress(addressName: personalAddress.addressName, nameSurname: personalAddress.nameSurname, city: personalAddress.city, county: personalAddress.county, district: personalAddress.district, address: personalAddress.address))
}
}

enter image description here

0 个答案:

没有答案