更改选项卡时清除查看数据SwiftUI

更改选项卡时清除查看数据SwiftUI,swift,swiftui,swiftui-tabview,Swift,Swiftui,Swiftui Tabview,我在其中一个选项卡中显示每日步骤信息。不幸的是,当我再次选择“步骤”选项卡时,它会在上一个选项卡的下方再添加一个相同的数据 我试图通过切换布尔值来解决这个问题。但这也无济于事 import SwiftUI import HealthKit struct StepView: View { private var healthStore: HealthStore? @State private var presentClipboardView = true @State

我在其中一个选项卡中显示每日步骤信息。不幸的是,当我再次选择“步骤”选项卡时,它会在上一个选项卡的下方再添加一个相同的数据

我试图通过切换布尔值来解决这个问题。但这也无济于事

import SwiftUI
import HealthKit

struct StepView: View {
    private var healthStore: HealthStore?
    @State private var presentClipboardView = true
    @State private var steps: [Step] = [Step]()
    init() {
        healthStore = HealthStore()
    }
    private func updateUIFromStatistics(_ statisticsCollection: HKStatisticsCollection) {
        let now = Date()
        let startOfDay = Calendar.current.startOfDay(for: now)
        statisticsCollection.enumerateStatistics(from: startOfDay, to: now) { (statistics, stop) in
            let count = statistics.sumQuantity()?.doubleValue(for: .count())
            let step = Step(count: Int(count ?? 0), date: statistics.startDate, wc: Double(count ?? 0 / 1000 ))
            steps.append(step)
        }
    }
    var body: some View {  
        VStack {
            ForEach(steps, id: \.id) { step in
                VStack {
                        HStack{
                            Text("WC")
                            Text("\(step.wc)")
                        }
                        HStack {
                            Text("\(step.count)")
                            Text("Total Steps")
                        }
                        Text(step.date, style: .date)
                            .opacity(0.5)
                        Spacer()
                }
            }
            .navigationBarBackButtonHidden(true)
        }
        .onAppear() {
            if let healthStore = healthStore {
                healthStore.requestAuthorization { (success) in
                    if success {
                        healthStore.calculateSteps { (statisticsCollection) in
                            if let statisticsCollection = statisticsCollection {
                                updateUIFromStatistics(statisticsCollection)
                            }
                        }
                    }
                }
            }
        }
        .onDisappear() {
            self.presentClipboardView.toggle()
        }
    }
}
阶跃模型

struct Step: Identifiable {
    let id = UUID()
    let count: Int?
    let date: Date
    let wc: Double
}
HealthStore文件

class HealthStore {
    var healthStore: HKHealthStore?
    var query: HKStatisticsCollectionQuery?
    init() {
        if HKHealthStore.isHealthDataAvailable() {
            healthStore = HKHealthStore()
        }
    }
    func calculateSteps(completion: @escaping (HKStatisticsCollection?) -> Void ) {
        let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
        let now = Date()
        let startOfDay = Calendar.current.startOfDay(for: now)
        let daily = DateComponents(day:1)
        let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: Date(), options: .strictStartDate)
        query = HKStatisticsCollectionQuery(quantityType: stepType, quantitySamplePredicate: predicate, options: .cumulativeSum, anchorDate: startOfDay, intervalComponents: daily)
        query!.initialResultsHandler = { query, statisticCollection, error in
            completion(statisticCollection)
        }
        if let healthStore = healthStore, let query = self.query {
            healthStore.execute(query)
        }
    }
    
    func requestAuthorization(completion: @escaping (Bool) -> Void) {
        let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
        guard let healthStore = self.healthStore else { return completion (false) }
        healthStore.requestAuthorization(toShare: [], read: [stepType]) { (success, error) in
            completion(success)
        }
    }
}
根据我的问题,这些都是相关文件。
提前感谢。

步骤用
@State
注释,这意味着即使重新绘制视图,也会保留该步骤

你永远不会重置它。您仅附加新步骤。尝试在
updateUIFromStatistics
中清除
步骤

private func updateUIFromStatistics(_ statisticsCollection: HKStatisticsCollection) {
    steps = [] // remove previous values
    let now = Date()
    let startOfDay = Calendar.current.startOfDay(for: now)
    statisticsCollection.enumerateStatistics(from: startOfDay, to: now) { (statistics, stop) in
        let count = statistics.sumQuantity()?.doubleValue(for: .count())
        let step = Step(count: Int(count ?? 0), date: statistics.startDate, wc: Double(count ?? 0 / 1000 ))
        steps.append(step)
    }
}

你的密码搞错了!我需要完整的代码,可能是因为。什么的完整代码?这是stepviewHealthStore的完整代码?步HealthStore()?steps?Healthstore很好用。我在接收数据方面没有问题。只是复制本身是个问题。如果你可以运行DOS代码,那么我也可以!你可以使用你给定的代码运行此应用程序吗?先试试你自己!我告诉过你3次,你给出了不清楚的信息,并要求回答!怎么可能呢