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 immedia
There is a very simple approach to handle your views' states and NavigationLinks
.
You can notify your NavigationLink
to execute itself by binding a tag
to it.
You can then set-unset the tag and take control of your NavigationLink
.
struct SwiftView: View {
@State private var actionState: Int? = 0
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: Text("Destination View"), tag: 1, selection: $actionState) {
EmptyView()
}
Text("Create Account")
.onTapGesture {
self.asyncTask()
}
}
}
}
func asyncTask() {
//some task which on completion will set the value of actionState
self.actionState = 1
}
}
Here we have binded the actionState
with our NavigationLink
, hence whenever the value of actionState
changes, it will be compared with the tag
associated with our NavigationLink
.
Your Destination View will not be visible until you set the value of actionState
equal to the tag
associated with our NavigationLink
.
Like this you can create any number of NavigationLinks
and can control them by changing just one Bindable
property.
Thanks, hope this helps.