xcode 当从数据库中提取这些值时,如何让Swift UI选取器更新为所选值?

qrjkbowd  于 2023-02-25  发布在  Swift
关注(0)|答案(1)|浏览(133)
    • 问题**我的选取器在选择新值时不会更新。当我单击/点击另一个值时,无论我选择什么,选取器都会关闭并显示"Austin"。

我正在使用:Xcode版本14.2
我提供了三段代码如下:
1.显示选取器的Swift UI视图
1.数据模型

  1. JSON数据文件
    下面是选取器的屏幕截图:

下面是使用Picker的代码:

import SwiftUI

struct AddEditExpenseView: View {
    
    @EnvironmentObject var destinationsModelData: DestinationsModelData
    
    var destinationIndex: Int {
        destinationsModelData.destinations.firstIndex(where: { $0.id == destination.id })!
    }
    
    var destination: Destination
    
    @State var selectedDestination: String = ""
    @State var saveDestinationFieldTextArray: [String] = []
    
    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Destination")) {
                    Picker("Destination", selection: $selectedDestination) {
                        ForEach(destinationsModelData.destinations) { destination in
                            Text(destination.name)
                        }
                    }
                    .onAppear {
                        selectedDestination = destination.name
                    }
                    .foregroundColor(.black)
                }
            }
            .formStyle(GroupedFormStyle())
            .accentColor(.black)
            .navigationBarTitle(Text("Add, Edit Expense"))
            .navigationBarBackButtonHidden(true)
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

struct AddEditExpenseView_Previews: PreviewProvider {
    static let destinationsModelData = DestinationsModelData()
    
    static var previews: some View {
        AddEditExpenseView(destination: destinationsModelData.destinations[0])
            .environmentObject(destinationsModelData)
    }
}

以下是目标模型:

import Foundation
import SwiftUI
import CoreLocation

struct Destination: Hashable, Codable, Identifiable {
    var id: Int
    var name: String
    var city: String
    var state: String
    var country: String
    var description: String
    var isOpen: Bool
    var isCompared: Bool
    
    private var imageName: String
    var image: Image {
        Image(imageName)
    }
    

    private var coordinates: Coordinates
    var locationCoordinate: CLLocationCoordinate2D  {
        CLLocationCoordinate2D (
            latitude: coordinates.latitude,
            longitude: coordinates.longitude)
    }
    
    
    struct Coordinates: Hashable, Codable {
        var latitude: Double
        var longitude: Double
    } 
}

我有一个存储数据的JSON文件:

[
    {
        "name": "Austin",
        "category": "Cities",
        "city": "Austin",
        "state": "Texas",
        "country": "USA",
        "id": 1001,
        "isOpen": true,
        "isCompared": true,
        "coordinates": {
            "longitude": -97.743057,
            "latitude": 30.267153
        },
        "description": "placeholder text",
        "imageName": "Austin_TX"
    },
    {destination2},
    {destination3}
]

我尝试使用@FetchRequest,但收到一条错误消息,指出不支持@FetchRequest
我尝试了.onChange,但没有成功。我无法进入编译阶段。

wmtdaxz3

wmtdaxz31#

要使Picker显示所选内容,@State var selectedDestination: String必须与Picker元素的.tag()的类型匹配。

Picker("Destination", selection: $selectedDestination) {
     ForEach(destinationsModelData.destinations) { destination in
         Text(destination.name).tag(destination.name)  // <-- here
     }
 }

这是假设所有destination.name都是唯一的。
更好的方法是使用.tag(destination.id)@State var selectedDestination: Int

相关问题