2

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.

Set Timer Screen

@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.

  • Provide more context so we can answer your question: https://stackoverflow.com/help/minimal-reproducible-example . (e.g. the whole view, something that would compile successfully on our own computers. Something minimal that works) – Mahdi BM Apr 17 '21 at 11:22
  • You have to set `movetoNextScreen` to 1 to trigger the transition. – vadian Apr 17 '21 at 11:30

1 Answers1

2

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() }
    )
Asperi
  • 228,894
  • 20
  • 464
  • 690