It seems so weird that I couldn't find anything. Isn't this a common problem?
So, I have a list in a NavigationView/-Stack
and have a .toolbar
add button.
When tapping that button it should open a DetailView
as a navigation detail view (not a .sheet
) to allow me to edit the new item, and also add it to the list. There seems to be navigationdestination(item:destination:)
which looks like what I want but that's beta iOS 17+.
Any ideas?
struct Item: Identifiable, Hashable {
let id = UUID()
var title: String = ""
}
struct EditView: View {
@Binding var item: Item
var body: some View {
TextField("Edit", text: $item.title)
}
}
struct ListView: View {
@State var todos = [Item(title: "abc")]
@State var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
List($todos) { $todo in
NavigationLink(todo.title) {
EditView(item: $todo)
}
}
// .navigationDestination(for: Item.self, destination: { item in
// // ERR: item is not a Binding
// EditView(item: item)
// })
.toolbar {
Button("+") {
let new = Item()
todos.append(new)
// path.append(todos.last) ??
}
}
}
}
}
This is my code. So I got the navigation link working for the list but am not sure how to also make it so it programmatically opens the navigation view when adding a new item.