Hi all,
quite new to SwiftUi, thanks to Paul I make some progress.
Having finished Project 10 of "SwiftUI in 100 days" I tried to write a first little app on my own reading from and writing to a little API I wrote to see the data from my photovoltaic system.
But I came across a problem I can't solve on my own (well, I did solve it, sort of, but my feeling is, that there must be a better solution).
It's all about master and detail view - and editing data in the detail view.
I wrote a most simple (and totally useless) app to demonstrate my problem.
When I switch to the detail view - how can I pass my variable in so that any change I make there gets reflected in the main view?
I just want to edit the data from the respective row.
What I tried is this, but there must be a better solution than to look for the UUID of the row?
struct ContentView: View {
class Test: ObservableObject {
struct Row {
var id: UUID
var name: String
var nummer: Int
init(name: String, nummer: Int) {
self.name = name
self.nummer = nummer
self.id = UUID()
}
}
@Published var title = "Test"
@Published var rows = [Row(name: "Item 1", nummer: 1), Row(name: "Item 2",nummer: 2), Row(name: "Item 3", nummer: 3), Row(name: "Item 4",nummer: 4)]
}
struct detailView: View {
@ObservedObject var test: Test
var itemID: UUID
var body: some View {
Form {
TextField(test.rows[getItemByID(itemID: itemID)].name, text: $test.rows[getItemByID(itemID: itemID)].name)
Text("\(test.rows[getItemByID(itemID: itemID)].nummer)" )
}
}
func getItemByID(itemID: UUID) -> Int {
return test.rows.firstIndex(where: {$0.id == itemID}) ?? 0
}
}
@StateObject var test = Test()
var body: some View {
NavigationView {
List {
Section("Title") {
Text(test.title)
.font(.headline)
}
Section("Title") {
ForEach(test.rows, id: \.nummer) { row in
NavigationLink {
detailView(test: test, itemID: row.id)
} label: {
Text(row.name)
}
}
}
}
}
}
}