【问题标题】:Having trouble binding a DatePicker to my EnvironmentObject variable无法将 DatePicker 绑定到我的 EnvironmentObject 变量
【发布时间】:2019-11-21 21:01:15
【问题描述】:

我在使用EnvironmentObject 并在详细视图中将变量绑定到我的数据时遇到了一些麻烦。我相信真相的来源@EnvironmentObject var userData: UserData。在列表视图中点击一行时,此详细视图应显示所选项目,您应该能够通过DatePicker 修改weight.weightdate。应用程序编译,当我使用下面的代码时,但当我尝试使用 DatePicker(selection: $userData.weights[weightIndex].day, in: ...Date(), displayedComponents: [.date,.hourAndMinute]) { 之类的东西来绑定我的数据时,我收到以下错误:

Type 'Any' has no member 'date'
Type 'Any' has no member 'hourAndMinute'

如果我添加 DatePickerComponents.date,我会在 Text("Date") 行得到 Type of expression is ambiguous without more context

struct Weight: Hashable, Codable, Identifiable {
    var id: Int
    var weightdate: Date
    var weight: Float = 0
    ...
}
struct WeightDetail: View {
    @EnvironmentObject var userData: UserData
    var weight: Weight
    var weightIndex: Int {
           userData.weights.firstIndex(where: { $0.id == weight.id })!
       }

    @State private var selectedDay = Date()

    var body: some View {
        Form {
            Section {
                //DatePicker(selection: $userData.weights[weightIndex].day, in: ...Date(), displayedComponents: [.date,.hourAndMinute]) {
                //ERROR Type 'Any' has no member 'date'
                //ERROR Type 'Any' has no member 'hourAndMinute'

                DatePicker(selection: $selectedDay, in: ...Date(), displayedComponents: [.date,.hourAndMinute]) {
                    Text("Date")
                    //also works: Text("\(weight.weightdate)")
                }
            }
            ...

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    我对数据做了一些假设。看场景委托。它应该提供一些关于环境对象的想法。

    import SwiftUI
    
    struct ContentView: View {
        var body: some View {
    
                WeightDetail(weight: Weight(id: 0, weightdate: Date()))
        }
    }
    
    
    struct WeightDetail: View {
        @EnvironmentObject var userData: UserData
          var weight: Weight
        var weightIndex: Int {
                    userData.weights.firstIndex(where: { $0.id == weight.id })!
            }
    
        @State private var selectedDay = Date()
    
        var body: some View {
            Form {
                Section {
                  //  DatePicker(selection: $userData.weights[weightIndex].day, in: ...Date(), displayedComponents: [.date,.hourAndMinute])
    
                                DatePicker(selection: $userData.weights[weightIndex].weightdate, in: ...Date(), displayedComponents: [.date, .hourAndMinute] ) {
                                    Text("Display")
                                }
                                    //{
                    //ERROR Type 'Any' has no member 'date'
                    //ERROR Type 'Any' has no member 'hourAndMinute'
    
    //                DatePicker(selection: $selectedDay, in: ...Date(), displayedComponents: [.date,.hourAndMinute]) {
    //                    Text("Date")
    //                    //also works: Text("\(weight.weightdate)")
    //                }
                }
                }
    
        }
    
    }
    
    
    struct Weight: Hashable, Codable, Identifiable {
        var id: Int
        var weightdate: Date
        var weight: Float = 0
    }
    
    class UserData: ObservableObject {
    
        @Published var weights = [Weight]()
    
        init(weights: [Weight]){
            self.weights = weights
        }
    }
    

    场景代理:

    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
        var window: UIWindow?
        var userData: UserData!
    
    
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
            let weight = Weight(id: 0, weightdate:  Date().advanced(by: -100000))
            let weights = [weight]
            userData =  UserData(weights: weights)
    
            let contentView = ContentView().environmentObject(userData)
    
    
            // Use a UIHostingController as window root view controller.
            if let windowScene = scene as? UIWindowScene {
                let window = UIWindow(windowScene: windowScene)
                window.rootViewController = UIHostingController(rootView: contentView)
                self.window = window
                window.makeKeyAndVisible()
            }
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多