0

I'm trying the iOS 16 beta, MacOS 13 beta, and Xcode 14 beta. I can't get TextField to edit a property within a @Binding struct. It works with a @State struct variable, but not binding. Anyone else seeing this problem?

I filed a Feedback with Apple a couple of days ago, but no response yet. I imagine they are swamped the week after WWDC.

  • MacOS Version 13.0 Beta (22A5266r)
  • Xcode Version 14.0 beta (14A5228q)

Here's a code example of the problem.

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationStack {
            List {
                VStack {
                    Image(systemName: "globe")
                        .imageScale(.large)
                        .foregroundColor(.accentColor)
                    Text("Hello, world!")
                }
                /// Click on Category List
                NavigationLink {
                    CatList()
                } label: {
                    Text("Category List")
                }
            }
        }
    }
}

struct CatList: View {
    /// Click on bar
    
    @State var catArray: [Categories] = [ Categories(id: UUID(), myName: "foo"), Categories(id: UUID(), myName: "bar")]
    
    var body: some View {
        List {
            ForEach($catArray) { $eachCat in
                NavigationLink {
                    CatDetails(thisCategory: $eachCat)
                } label: {
                    Text(eachCat.myName)
                }
            }
        }
    }
}

struct CatDetails: View {
    
    @Binding var thisCategory: Categories
    
    var body: some View {
        Form {
            /// Cannot edit bar.
            TextField("Name:", text: $thisCategory.myName)
        }
    }
    
}

struct Categories: Identifiable, Hashable {
    var id: UUID
    var myName: String
}
adamek
  • 2,324
  • 3
  • 24
  • 38
  • 3
    NavigationStack does not transfer bindings (see this topic for details https://stackoverflow.com/a/72584743/12299030. I think you mixed patterns. For `NavigationLink(destination` use `NavigationView` and it works, or if you use `NavigationStack` then use corresponding `NavigationLink(value:`+`navigationDestination`+`ObservableObject` as view model. And yes, code in question does not work at iOS 16 as well (but I did not even expect that, just in case). – Asperi Jun 14 '22 at 04:05
  • Thanks. That linked post got it. I think NavigationStack is still a work in progress. Your ObservableObject idea is another working alternative. – adamek Jun 14 '22 at 04:47

0 Answers0