Search code examples
swiftswiftuiwatchos

How to fix TextField popping back to root on cancel?


I have a simple test watchOS application with a TextField in a secondary view (navigated to from a NavigationLink). However, when the TextField is canceled or submitted, it will pop back out to the root view instead of staying in the current view. I can't find any information on this anywhere else. Any fixes?

ContentView:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView{
            NavigationLink("what", destination: DestinationView())
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

DestinationView:

import SwiftUI

struct DestinationView: View {
    @State private var message: String = ""

    var body: some View {
        TextField(
                "Send Something...",
                text: $message
            )
    }
}

struct DestinationView_Previews: PreviewProvider {
    static var previews: some View {
        DestinationView()
    }
}

Solution

  • I found the issue..

    I was using a NavigationView, which is deprecated. I removed it and now it's working as intended. (XCode 13.2.1, watchOS 8.3)

    *facepalm*