首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI @AppStorage不会在函数中刷新

SwiftUI @AppStorage不会在函数中刷新
EN

Stack Overflow用户
提问于 2021-06-01 20:54:13
回答 1查看 181关注 0票数 1

我尝试在每个.onAppear中重新加载数据,但是如果我在SettingsView中更改@AppStorage nearMeter的值,它不会更新reloadNearStops函数中的值,也不会使用之前的@AppStorage值。

代码语言:javascript
复制
    struct SettingsView: View {
        @AppStorage(“nearMeter”) var nearMeter: Int = 1
        @State var meters = ["100 m","200 m","400 m","500 m","750 m","1 km"]
    
    var body: some View {


    ………
    
                            Picker(selection: $nearMeter, label: HStack {
                                    Text(NSLocalizedString(“near_stops_distance”, comment: ""))
                            }) {
                                ForEach(0 ..< meters.count, id: \.self) {
                                        Text(meters[$0])
                                }
                            }}}



    struct FavouritesView: View {
 @AppStorage(“nearMeter”) var nearMeter: Int = 1
        
            func reloadNearStops(nearMeter: Int) {
                
                print(nearMeter)
                readNearStopsTimeTable.fetchTimeTable(nearMeter:        getLonLatSpan(nearMeter: nearMeter), lat: (locationManager.lastLocation?.coordinate.latitude)!, lon: (locationManager.lastLocation?.coordinate.longitude)!)
            }
            
            
            func getLonLatSpan(nearMeter: Int) -> Double {
                let meters = [100,200,400,500,750,1000]
                
                if nearMeter < meters.count {
                    return Double(meters[nearMeter]) * 0.00001
                }
                else {
                    return 0.001
                }
            }
        var body: some View {
    
      
    .....
        
        ……….
        .onAppear() {
                    
                    if locationManager.lastLocation?.coordinate.longitude != nil {
                            if hasInternetConnection {
                                reloadNearStops(nearMeter: nearMeter)
                            }
                    }
        
        }}
EN

回答 1

Stack Overflow用户

发布于 2021-06-01 21:31:14

AppStorage不会调用函数,但是当AppStorage发生变化时,onChange可以调用函数。

代码语言:javascript
复制
struct StorageFunctionView: View {
    @AppStorage("nearMeter") var nearMeter: Int = 1
    @State var text: String = ""
    var body: some View {
        VStack{
            Text(text)
            Button("change-storage", action: {
                nearMeter = Int.random(in: 0...100)
            })
        }
        //This will listed for changes in AppStorage
        .onChange(of: nearMeter, perform: { newNearMeter in
            //Then call the function and if you need to pass the new value do it like this
            fetchSomething(value: newNearMeter)
        })
    }
    func fetchSomething(value: Int)  {
        text = "I'm fetching \(value)"
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67789264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档