I am currently using tab views to navigate between 4 views in my project, and the initial view is my profileView, where I want the user to input their information and goals, such as age, height etc. **However I am really struggling to find good examples and instructions on how to get user input and set my environment object to that user input. I was thinking of just using forms, but it seems unnecessary and im not too sure how to change the environment object to what the user has set. Does anyone have any good documentation they can refer me to, or show me an example of how to get user input and set my environment object to that data. (I am also not sure if this is necessary, as I will also later be storing it in userdefaults or coredata.
This is my model class which is my class data for the user. Ignore the set values at the bottom for now, they will all be set to empty when the project is in later development.
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)
}
So when I am in my profile view how do i get the user input and set my environment object to that data?
The above has been solved, thanks to the help provides so far, however using the method linked below I get the following, but I am not sure how to change the value of my double environment object to the fatInputString:
New code to get input from user
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
}
But how do i set my environment object
self.person.personDailyCalorieGoals.fatGoal
equal to
self.inputString
from the above code }