I am trying to navigate to another view by pressing on Toolbar button and it does not respond. I embedded the Button inside NavigationLink. How else can I do this?
var body: some View {
NavigationView {
List {
ForEach(items) { item in
NavigationLink {
DisplayContactView()
} label: {
Text(item.timestamp!, formatter: itemFormatter)
}
}
.onDelete(perform: deleteItems)
}
.navigationTitle("Contacts")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
.foregroundColor(.black)
}
ToolbarItem {
NavigationLink(destination: AddContactView()) {
Button(action: { }) {
Label("Add Contact", systemImage: "plus")
.foregroundColor(.black)
}
}
}
Text("Select a Contact")
}
}
Is this what you're looking for:
struct ContentView: View {
// A new state var on false by default
@State var showAddView = false
var body: some View {
NavigationView {
List {
// ForEach + NavigationLink
}
.navigationTitle("Contacts")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Add", action: { showAddView = true })
// When the var is true the content is presented
// You can also use .toggle() instead of = true
.fullScreenCover(isPresented: $showAddView, content: { AddView() })
}
}
}
}
}
Here's another example with a .sheet
instead of a .fullScreenCover
: https://www.hackingwithswift.com/quick-start/swiftui/how-to-present-a-new-view-using-sheets