Reputation: 155
I'm trying to make navigation link, here I'm creating NavigationLink
with isActive
based on State
variable isLoggedIn
. But without setting isLoggedIn
true
getting navigating to next screen.
Email Textfield
which is wrong.My expectation is it should navigate only after isLoggedIn
setting to true
.
struct ContentView: View {
@State private var isLoggedIn = false
@State private var email = ""
var body: some View {
NavigationView {
NavigationLink(destination: Text("Second View"), isActive: $isLoggedIn) {
VStack {
TextField("Email", text: $email)
.frame(maxWidth: .infinity, alignment: .leading)
.border(.gray, width: 1)
.foregroundColor(.blue)
Button("Send") {
isLoggedIn = true
}
}
.padding()
}
}
}
}
Upvotes: 1
Views: 103
Reputation: 155
Here it's working fine with this
struct MoviesListView: View {
@State var navigate = false
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: Text("Hi"), isActive: $navigate) {
Button("Add") {
navigate.toggle()
}
}
}
}
}
}
Upvotes: 0
Reputation: 258345
The expectation is wrong, NavigationLink
handles user input independently (but also, additionally, can be activated programmatically).
In this scenario, to leave only programmatic activation, we need to hide navigation link, like
NavigationView {
VStack {
TextField("Email", text: $email)
.frame(maxWidth: .infinity, alignment: .leading)
.border(.gray, width: 1)
.foregroundColor(.blue)
Button("Send") {
isLoggedIn = true
}
.background(NavigationLink(destination: // << here !!
Text("Second View"), isActive: $isLoggedIn) { EmptyView() })
}
.padding()
}
Upvotes: 1