View not updating on iOS 17, working on previous iOS

Hi all, I have some code that works on iOS 14 up to 16 and now suddenly doesn't work on iOS 17 so I guess it's a regression. Here's a snippet of a minimal reproducible example. SubView is supposed to update correctly when the button is pressed but that's not happening on iOS 17. onChange is not called either.

struct ContentView: View {
  @ObservedObject var viewModel: ViewModel
  
  var body: some View {
    SubViewContainer(wrapper: $viewModel.wrapper)
  }
}

struct SubViewContainer: View {
  @Binding var wrapper: ValueWrapper
  
  var body: some View {
    SubView(value: $wrapper.value)
  }
}

struct SubView: View {
  @Binding var value: Bool
  
  var body: some View {
    Button(action: {
      value.toggle()
    }) {
      Text(value ? "true" : "false")
    }
  }
}

class ViewModel: ObservableObject {
  @Published var wrapper = ValueWrapper()
}

class ValueWrapper: ObservableObject {
  @Published var value = true
}

Any clue what is going on?

  • try using "struct ValueWrapper { var value = true } " instead of nesting "ObservableObject". Assuming you have declared "@StateObject var viewModel = ViewModel()" in the ContentView parent.

Add a Comment