Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在SwiftUI中将环境对象设置为用户输入?_Ios_Swift_Swiftui - Fatal编程技术网

Ios 如何在SwiftUI中将环境对象设置为用户输入?

Ios 如何在SwiftUI中将环境对象设置为用户输入?,ios,swift,swiftui,Ios,Swift,Swiftui,我目前正在使用选项卡视图在我的项目中的4个视图之间导航,初始视图是我的profileView,我希望用户在其中输入他们的信息和目标,例如年龄,高度等**但是我真的很难找到关于如何获得用户输入并将我的环境对象设置为该用户输入的好例子和说明。我只是想使用表单,但似乎没有必要,而且我也不太确定如何将环境对象更改为用户设置的对象。有没有人有什么好的文档可以给我参考,或者给我展示一个如何获取用户输入并将我的环境对象设置为该数据的示例。(我也不确定这是否必要,因为我稍后还会将其存储在userdefaults

我目前正在使用选项卡视图在我的项目中的4个视图之间导航,初始视图是我的profileView,我希望用户在其中输入他们的信息和目标,例如年龄,高度等**但是我真的很难找到关于如何获得用户输入并将我的环境对象设置为该用户输入的好例子和说明。我只是想使用表单,但似乎没有必要,而且我也不太确定如何将环境对象更改为用户设置的对象。有没有人有什么好的文档可以给我参考,或者给我展示一个如何获取用户输入并将我的环境对象设置为该数据的示例。(我也不确定这是否必要,因为我稍后还会将其存储在userdefaults或coredata中

这是我的模型类,它是我为用户提供的类数据。暂时忽略底部的设置值,当项目处于后期开发中时,它们都将设置为空

class UserInfoModel: ObservableObject {
    
    
    struct UserInfo: Identifiable {
        var id = UUID()
        var firstName: String
        var lastName: String
        var height: Int
        var weight: Int
        var gender: String
        var age: Int
        
    }
    
    struct DailyCalorieGoals: Identifiable{
        var id = UUID()
        var calorieGoal: Int
        var fatGoal: Int
        var proteinGoal: Int
        var carbGoal: Int

    }
    
    struct CurrentCalorieProgress: Identifiable{
        var id = UUID()
        var calorieProgress: Int
        var fatProgress: Int
        var carbProgress: Int
    }
    
    @Published var personUserInfo = UserInfo.init(firstName: "", lastName:"", height: 0, weight: 0, gender: "", age: 0)
    @Published var personDailyCalorieGoals = DailyCalorieGoals.init(calorieGoal: 2400, fatGoal: 0, proteinGoal: 0, carbGoal: 0)
    @Published var personCurrentCalorieProgress = CurrentCalorieProgress.init(calorieProgress: 0, fatProgress: 0, carbProgress: 0)
    
   
}
因此,当我在我的纵断面图中时,我如何获得用户输入并将我的环境对象设置为该数据

由于目前提供的帮助,上述问题已经解决,但是使用下面链接的方法,我得到了以下结果,但我不确定如何将double environment对象的值更改为fatInputString:

获取用户输入的新代码

Text("Fat: \(self.person.personDailyCalorieGoals.fatGoal, specifier: "%.0f")g")
                            }
                            TextField("Enter new fat goal (g)", text: $fatInputString ).keyboardType(.numberPad)
                                .onReceive(Just(fatInputString)) { newValue in
                                                let filtered = newValue.filter { "0123456789".contains($0) }
                                                if filtered != newValue {
                                                    self.fatInputString = filtered
                                                }
但是如何设置环境对象呢

self.person.personDailyCalorieGoals.fatGoal 
等于

self.inputString 
根据上述代码
}

不需要使用
表单
——它只会使用户输入元素的格式在iOS/macOS上更加标准化

无论您是否使用
表单
,您都将使用各种用户输入元素,并在
用户信息模型
上为它们分配绑定。SwiftUI/Combine使使用
$
操作符实现这一点非常容易

下面是一个如何获取对几个字段的输入的示例。您可以看到,当字段信息更改时,该值会反映在
ContentView
拥有的
EnvironmentObject
中--它会在
EditorView
上方的顶部显示该值

struct ContentView : View {
    @ObservedObject var userInfo = UserInfoModel()
    
    var body: some View {
        VStack {
            Text("Name: \(userInfo.personUserInfo.firstName)")
            Text("Weight: \(userInfo.personUserInfo.weight)")
            Text("Carb: \(userInfo.personCurrentCalorieProgress.carbProgress)")
            EditorView().environmentObject(userInfo)
        }
    }
}

struct EditorView : View {
    @EnvironmentObject private var userInfo : UserInfoModel
    
    var body: some View {
        Form {
            TextField("First name", text: $userInfo.personUserInfo.firstName)
            Stepper("Weight", value: $userInfo.personUserInfo.weight, in: 0...500)
            Stepper("Carb progress", value: $userInfo.personCurrentCalorieProgress.carbProgress, in: 0...1000, step: 10)
        }
    }
}

根据评论进行更新: 编辑以显示有关数字文本输入的请求:

struct EditorView : View {
    @EnvironmentObject private var userInfo : UserInfoModel
    @State var fatInputString = "" //<-- Here
    
    var body: some View {
        Form {
            TextField("First name", text: $userInfo.personUserInfo.firstName)
            Stepper("Weight", value: $userInfo.personUserInfo.weight, in: 0...500)
            Stepper("Carb progress", value: $userInfo.personCurrentCalorieProgress.carbProgress, in: 0...1000, step: 10)
            Text("Fat: \(userInfo.personDailyCalorieGoals.fatGoal)g") //<-- Here
            TextField("Enter new fat goal (g)", text: $fatInputString ).keyboardType(.numberPad)
                .onReceive(Just(fatInputString)) { newValue in
                    let filtered = newValue.filter { "0123456789".contains($0) }
                    if filtered != newValue {
                        self.fatInputString = filtered
                    }
                    if let goal = Int(fatInputString), goal != userInfo.personDailyCalorieGoals.fatGoal {  //<-- Here
                        userInfo.personDailyCalorieGoals.fatGoal = goal
                    }
                }
        }
        
    }
}
结构编辑器视图:视图{ @EnvironmentObject私有变量userInfo:UserInfoModel
@State var fatInputString=”“//这是一个非常棒的答案,非常感谢!但是,我还有最后一个问题。如何让用户使用编辑文本或类似的内容来调整int值$userInfo.personUserInfo.weight?谢谢!对于在TextField中接受int,有一些非常好的答案(这需要一点修改)这里:如果您需要针对您的具体情况对其进行说明,请告诉我。好的,这样我就能够获得用户的输入,但是在使用以下命令时,我会遇到以下错误:Let fatInput=String(self.person.personDailyCalorieGoals.fatGoal)然后尝试设置文本字段(“输入Fat”,文本:fatInput)您实际上没有记录错误,但我假设它告诉您必须使用绑定,而不仅仅是文本字段中的字符串。请查看我链接到的答案以及它如何使用@State变量,并查看它使用的
$
运算符。如果需要,您可以编辑此问题以显示您的代码,这样我就可以给出更具体的建议。只需编辑我的答案,保留原始内容以备其他人需要,非常感谢您迄今为止的帮助!