swiftui-绑定var仅在第一次更新原始值,然后它不再更新了

发布于 2025-02-08 19:12:58 字数 2928 浏览 2 评论 0原文

我有一个更新@binding值的选择器,该值链接到原始@State值。问题在于它仅在我第一次更改它时才会更新,然后总是这样。不仅在表格中,而且在ContentView中,这是声明@State变量的原始视图。 这是一个显示问题的视频,这是代码:

contentView:

struct ContentView: View {
    
    @State var cityForTheView = lisbon
    @State var showCitySelection = false

    var body: some View {
        VStack {
            //View
        }
        .sheet(isPresented: $showCitySelection) {
            MapView(cityFromCV: $cityForTheView)
        }
    }
}

mapview:mapview:

struct MapView: View {
    
    @Environment(\.presentationMode) var presentationMode
    @Binding var cityFromCV : CityModel
    
    @State var availableCities = [String]()
    @State var selectedCity = "Lisbon"
    
    var body: some View {
        NavigationView {
            ZStack {
            VStack {
                Form {
                    HStack {
                        Text("Select a city:")
                        Spacer()
                        Picker("", selection: $selectedCity) {
                            ForEach(availableCities, id: \.self) {
                                            Text($0)
                                        }
                                    }
                        .accentColor(.purple)

                    }
                    .pickerStyle(MenuPickerStyle())
                    

                    Section {
                        Text("Current city: \(cityFromCV.cityName)")
                    }
                }
            }
        }
        }
        .interactiveDismissDisabled()
        .onAppear {
            availableCities = []
            for ct in cities {
                availableCities.append(ct.cityName)
            }
        }
        .onChange(of: selectedCity) { newv in
            self.cityFromCV = cities.first(where: { $0.cityName == newv })!
        }
    }
}

citymodel:citymodel:

class CityModel : Identifiable, Equatable, ObservableObject, Comparable {
    
    var id = UUID()
    var cityName : String
    var country : String
    var imageName : String

    init(cityName: String, country: String, imageName : String) {
        self.cityName = cityName
        self.country = country
        self.imageName = imageName
    }
    
    static func == (lhs: CityModel, rhs: CityModel) -> Bool {
        true
    }
    
    static func < (lhs: CityModel, rhs: CityModel) -> Bool {
        true
    }

}
  
var cities = [
    lisbon,
    CityModel(cityName: "Madrid", country: "Spain", imageName: "Madrid"),
    CityModel(cityName: "Barcelona", country: "Spain", imageName: "Barcelona"),
    CityModel(cityName: "Paris", country: "France", imageName: "Paris")
]

var lisbon = CityModel(cityName: "Lisbon", country: "Portugal", imageName: "Lisbon")

什么是什么我做错了吗?

I have a Picker that updates a @Binding value, that is linked to the original @State value. The problem is that it gets updated only the first time I change it, and then it always remains like that. Not only in the sheet, but also in the ContentView, which is the original view in which the @State variable is declared. Here's a video showing the problem, and here's the code:

ContentView:

struct ContentView: View {
    
    @State var cityForTheView = lisbon
    @State var showCitySelection = false

    var body: some View {
        VStack {
            //View
        }
        .sheet(isPresented: $showCitySelection) {
            MapView(cityFromCV: $cityForTheView)
        }
    }
}

MapView:

struct MapView: View {
    
    @Environment(\.presentationMode) var presentationMode
    @Binding var cityFromCV : CityModel
    
    @State var availableCities = [String]()
    @State var selectedCity = "Lisbon"
    
    var body: some View {
        NavigationView {
            ZStack {
            VStack {
                Form {
                    HStack {
                        Text("Select a city:")
                        Spacer()
                        Picker("", selection: $selectedCity) {
                            ForEach(availableCities, id: \.self) {
                                            Text($0)
                                        }
                                    }
                        .accentColor(.purple)

                    }
                    .pickerStyle(MenuPickerStyle())
                    

                    Section {
                        Text("Current city: \(cityFromCV.cityName)")
                    }
                }
            }
        }
        }
        .interactiveDismissDisabled()
        .onAppear {
            availableCities = []
            for ct in cities {
                availableCities.append(ct.cityName)
            }
        }
        .onChange(of: selectedCity) { newv in
            self.cityFromCV = cities.first(where: { $0.cityName == newv })!
        }
    }
}

CityModel:

class CityModel : Identifiable, Equatable, ObservableObject, Comparable {
    
    var id = UUID()
    var cityName : String
    var country : String
    var imageName : String

    init(cityName: String, country: String, imageName : String) {
        self.cityName = cityName
        self.country = country
        self.imageName = imageName
    }
    
    static func == (lhs: CityModel, rhs: CityModel) -> Bool {
        true
    }
    
    static func < (lhs: CityModel, rhs: CityModel) -> Bool {
        true
    }

}
  
var cities = [
    lisbon,
    CityModel(cityName: "Madrid", country: "Spain", imageName: "Madrid"),
    CityModel(cityName: "Barcelona", country: "Spain", imageName: "Barcelona"),
    CityModel(cityName: "Paris", country: "France", imageName: "Paris")
]

var lisbon = CityModel(cityName: "Lisbon", country: "Portugal", imageName: "Lisbon")

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乜一 2025-02-15 19:12:58

问题是:

在第35行:您使用选定的牙科存储从您的选择器中存储所选数据。

 Picker("", selection: $selectedCity) {

然后在第46行:您使用CityFromcv.CityName显示数据,该数据将始终显示错误的数据,因为您将所选数据存储在变量选择的位置中。

 Section {
  Text("Current city: \(cityFromCV.cityName)")
 }

解决方案:只需从CityFromcv.CityName更改为选定的CityName即可。

The problem are:

at line 35: you use selectedCity to store the selected data from your picker.

 Picker("", selection: $selectedCity) {

then at your line 46: you use cityFromCV.cityName to display the data which will always show the wrong data because you store your selected data in the variable selectedCity.

 Section {
  Text("Current city: \(cityFromCV.cityName)")
 }

Solution: Just change from cityFromCV.cityName to selectedCity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文