I started learning Swift and ran into the following problem. There are two Views – TrainingEditor and CategoryEditor. In TreningEditor, when you click on the “Add Training” button, the View CategoryEditor is called, where the necessary data for $trening.catTrening is saved, but when the form is closed, the data is not saved and the ForEach($trening.catTrening) cycle is empty
How to save and transfer data when closing a form in TreningEditor ($trening.catTrening)?
struct TreningEditor: View {
@Binding var trening: TreningItem
@State var isNew = false
@State var isNewCat = false
@Environment(\.dismiss) private var dismiss
@FocusState var focusedExercise: Item?
@FocusState var focusedExerciseList: Exercise?
@State private var isPickingSymbol = false
@State private var isAddingNewCategory = false
@State private var newCategory = Item()
var body: some View {
List {
HStack {
Button {
isPickingSymbol.toggle()
} label: {
Image(systemName: trening.symbol)
.imageScale(.large)
.foregroundColor(Color(trening.color))
}
.buttonStyle(.plain)
.padding(.horizontal, 5)
TextField("New Trening", text: $trening.treningName)
.font(.title2)
Button {
isPickingSymbol.toggle()
}
label: {
HStack {
Image(systemName: "plus")
} .buttonStyle(.plain)
}
}
.padding(.top, 5)
DatePicker("Date", selection: $trening.date)
.labelsHidden()
.listRowSeparator(.hidden)
Text("Exercises")
.fontWeight(.bold)
ForEach($trening.catTrening)
{
$item in Text("Test Trening: \(item.category)")
}
.onDelete(perform: { indexSet in
trening.catTrening.remove(atOffsets: indexSet)
})
Button {
isNewCat = true
CategoryEditor(trening: $newCategory, isNewCat: isNewCat)
trening.catTrening.append(newCategory)
} label: {
HStack {
Image(systemName: "plus")
Text("Add Trening")
}
}
.buttonStyle(.borderless)
.sheet(isPresented: $isNewCat) {
// isNewCat = true
CategoryEditor(trening: $newCategory, isNewCat: isNewCat)
}
}
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
#endif
.sheet(isPresented: $isPickingSymbol) {
SymbolPicker(trening: $trening)
}
}
}
struct CategoryEditor: View {
@Binding var trening: Item
@State var isNewCat = false
@Environment(\.dismiss) private var dismiss
@FocusState var focusedExercise: Exercise?
@State private var isPickingSymbol = false
@State private var isAddingNewCategory = false
@State private var newCategory = Item()
@State var category: String = "Ноги"
var categoryList = ["Руки","Ноги"]
@State var tag: String = "Не выбрано"
var tagList = ["Не выбрано","Икры","Пресс", "Брасс"]
var body: some View {
NavigationView
{
List {
HStack(alignment: .center) {
Text("New Category")
}
.padding(.top, 5)
Picker(selection: $category, label: Text("Тренируем"))
{
ForEach(categoryList, id: \.self)
{
Text($0)
}
} .listRowSeparator(.hidden)
Picker(selection: $tag, label: Text("Тег"))
{
ForEach(tagList, id: \.self)
{
Text($0)
}
} .listRowSeparator(.hidden)
Text("Exercises")
.fontWeight(.bold)
ForEach($trening.exercise)
{
$item in ExerciseAddRow(exercise: $item, focusedExercise: $focusedExercise, exerciseSelect: "Не выбрано", exerciseSelectList: ["Не выбрано","Жим ногами"])
}
.onDelete(perform: { indexSet in
trening.exercise.remove(atOffsets: indexSet)
})
Button {
let newExercise = Exercise(exerciseName: "",
set: 0, rep: 0,
link: "",
exerciseCat: "",
isNew: true )
trening.exercise.append(newExercise)
focusedExercise = newExercise
} label: {
HStack {
Image(systemName: "plus")
Text("Add Exercise")
}
}
.buttonStyle(.borderless)
}
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
#endif
.sheet(isPresented: $isPickingSymbol) {
// SymbolPicker(trening: $trening)
}.toolbar{
ToolbarItem(placement: .cancellationAction){
Button("Cancel", action: {dismiss()}).foregroundColor(Color(trening.color))
}
ToolbarItem(placement: .confirmationAction){
Button("Save", action: {}).foregroundColor(Color(trening.color))
}
}
}
}
}