Reputation: 3378
Can you have multiple NavigationLink
s in SwiftUI? The following only displays the first Link:
struct Test : View {
var body: some View {
NavigationView {
NavigationLink(destination: Text("First")) {
Text("Visible")
}
NavigationLink(destination: Text("Second")) {
Text("Invisible")
}
//EDIT: Also Invisible
Text("Not rendered")
}
}
}
EDIT: Turns out everything under the first NavigationLink
is not displayed
Upvotes: 6
Views: 8184
Reputation: 11636
see apple example, that has a clean impelementation. Here: https://developer.apple.com/documentation/swiftui/navigationlink
If You need to
add navigation on buttons, You have simply to set a state var un button action, (button will be where You put Text("xxvisible") ), say it selection.
in Apple code:
...
detail: { if let color = selection { ColorDetail(color: color) } else { Text("Pick a color") } }
replace color with Your selection and use else or switch/case.
Upvotes: 1
Reputation: 209
Look you can definitely have multiple NavigationLinks but here you are doing one things wrong.
The body property returns a single View but here you are trying to return more than one views, which causes the error.
To Solve this issue we put them inside another View like a VStack or a HStack, like shown above in the answer given by kontiki.
Upvotes: 2
Reputation: 40499
Put your views inside a VStack:
struct Test : View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: Text("First")) {
Text("Visible")
}
NavigationLink(destination: Text("Second")) {
Text("Invisible")
}
//EDIT: Also Invisible
Text("Not rendered")
}
}
}
}
Upvotes: 10