1

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.

  • also, it's navigating on tap of 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() } } } } 
1
  • You can use a NavigationLink with EmptyView for label, and vstack with text and button. Commented May 10, 2022 at 13:58

2 Answers 2

1

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() } 
Sign up to request clarification or add additional context in comments.

Comments

0

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() } } } } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.