I've read a lot here about navigation in SwiftUI and tried a couple of things, but nothing is working as desired.
I am developing a watch app in which I have to use two TextField. I only want to move on next screen when both field are not empty. Here is my code.
@State var movetoNextScreen: Int? = nil
NavigationLink(destination: WaterTemprature(), tag: 1,
selection: $movetoNextScreen) {
ZStack {
Text("Next")
.font(.system(size: 12, weight: .semibold))
.frame(alignment: .center)
Button(action: nextBtnPressed) {
if !check_minute_and_seconds_are_empty() {
// move to next screen.
movetoNextScreen = 1
}
}.opacity(0)
}
}
But when I pressed the navigation link, Button action did not call and control move to next screen. So I want to find any method that can first check the textfields and then move to next view.
Try in opposite direction - put button in front (with needed redesign) and place link in background with programmatic activation, like
Button(action: {
if !check_minute_and_seconds_are_empty() {
movetoNextScreen = 1 // << activate link
}
}) {
Text("Next")
.font(.system(size: 12, weight: .semibold))
.frame(alignment: .center)
}
.background(
NavigationLink(destination: WaterTemprature(), tag: 1,
selection: $movetoNextScreen) { EmptyView() }
)