I want to run account creation logic and then, if successful, transition to the destination view. Otherwise, I'll present an error sheet. NavigationLink transitions immediately to the destination view on tap.
I can get it to work if I create a phantom NavigationLink using the isActive overload and an empty string as the text (which creates a view with a zero frame). Then I toggle the isActive property with a Button presented to the user that runs the account creation logic first and at the end of the chain toggles the NavigationLink to active. I am inside a NavigationView.
@State private var isActive: Bool = false
NavigationView {
// Name, Email, Password Textfields here
// Button to run account creation logic:
Button(action: {
// Account creation promise chain here, then...
self.isActive.toggle()
}) {
Text("Create Account")
}
// Phantom navigation link:
NavigationLink("", destination: VerifyEmailView(email: email), isActive: self.$isActive)
}
Is there a better way to do this? It seems bad practice to trigger running the account creation logic from a button, and then activate a phantom navigation link to transition to the next screen.
Building off of Mohit's answer, making a little more Swifty with an enum that encapsulates screen state:
You can wrap your Destination View in a lazy view to prevent the immediate invocation. Here's an example:
Eventually, invoke it like this:
I've written a full blog post covering this a few other pitfalls of NavigationLinks in SwiftUI. Refer here.
There is a very simple approach to handle your views' states and
NavigationLinks
. You can notify yourNavigationLink
to execute itself by binding atag
to it. You can then set-unset the tag and take control of yourNavigationLink
.}
Here we have binded the
actionState
with ourNavigationLink
, hence whenever the value ofactionState
changes, it will be compared with thetag
associated with ourNavigationLink
.Your Destination View will not be visible until you set the value of
actionState
equal to thetag
associated with ourNavigationLink
.Like this you can create any number of
NavigationLinks
and can control them by changing just oneBindable
property.Thanks, hope this helps.