I am utilizing the brand new SwiftData launched in ios 17 to create an inventory of sections and rows.
On passing the sorted merchandise knowledge to alert on clicking the button in first row of each part, the information is inconsistent.
}).alert("Add Process - (process.title)", isPresented: $showingSubtaskAlert) {
After I’m opening a alert with the above code, the duty knowledge handed will not be at all times the identical because the row the button was clicked. Generally I get process knowledge of various row.
Full code block for reference. Please add any ideas to strive.
@Surroundings(.modelContext) personal var modelContext
@Question(filter: #Predicate { $0.subtasks.depend > 0 }, kind: TaskData.createdOn, order: .ahead) personal var duties: [TaskData]
@State personal var selectedRow: (part: String, row: String) = ("","")
@State personal var showingTaskAlert = false
@State personal var taskTitle = ""
@State personal var showingSubtaskAlert = false
@State personal var subTaskTitle = ""
var physique: some View {
NavigationView {
Checklist() {
ForEach(duties) { process in
Part() {
TaskRow(
title: process.title,
isSectionSelected: process.title == selectedRow.part,
onTap: {
selectedRow = (process.title, process.subtasks.first!.title)
},
onAddButtonTapped: {
showingSubtaskAlert = true
}).alert("Add Process - (process.title)", isPresented: $showingSubtaskAlert) {
TextField("Title", textual content: $subTaskTitle)
Button("Accomplished", motion: {
let s = subTaskTitle.trimmingCharacters(in: .whitespacesAndNewlines)
var isNotEqual = true
for subtask in process.subtasks {
if subtask.title == s {
isNotEqual = false
break
}
}
if s != "" && isNotEqual {
process.subtasks.append(TaskData(title: s))
strive? modelContext.save()
}
subTaskTitle = ""
})
Button("Cancel", position: .cancel) { }
}
ForEach(process.subtasks.sorted { $0.createdOn > $1.createdOn }) { subtask in
SubTaskRow(
title: subtask.title,
isSectionSelected: process.title == selectedRow.part,
isSelected: selectedRow == (process.title, subtask.title),
onTap: {
selectedRow = (process.title, subtask.title)
})
}
}
}