Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

278
Vistas
How to pass data between ViewModels in SwiftUI

I have this use case where I have a parent view and a child view. Both of the views have their own corresponding ViewModels.

ParentView:

struct ParentView: View {

  @StateObject var parentViewModel = ParentViewModel()

  var body: some View {
    NavigationView {
        List {
            TextField("Add Name", text: $parentViewModel.newListName)
            NavigationLink(destination: ChildView()) {
                Label("Select Products", systemImage: K.ListIcons.productsNr)
            }
        }
    }
}

ParentViewModel:

class AddListPopoverViewModel: ObservableObject {

  @Published var newListName: String = ""

  func saveList() {
    // some logic to save to CoreData, method would be called via a button
    // how do I reference "someString" from ChildViewModel in this ViewModel?
  }
}

ChildView:

struct ChildView: View {

    @StateObject var childViewModel = ChildViewModel()

    var body: some View {
        NavigationView {
            List{
                Text("Some element")
                    .onTapGesture {
                        childViewModel.alterData()
                    }      
            }
        }
    }
}

ChildViewModel:

class ChildViewModel: ObservableObject {
    @Published var someString: String = ""

    func alterData() {
       someString = "Toast"
    }
}

My question now is, how do I pass the new value of "someString" from ChildViewModel into the ParentViewModel, in order to do some further stuff with it? I've tried to create a @StateObject var childViewModel = ChildViewModel() reference in the ParentViewModel, but that does obviously not work, as this will create a new instance of the ChildViewModel and therefore not know of the changes made to "someString"


Solution: As proposed by Josh, I went with the approach to use a single ViewModel instead of two. To achieve this, the ParentView needs a .environmentObject(T) modifier.

ParentView:

struct ParentView: View {

  @StateObject var parentViewModel = ParentViewModel()

  var body: some View {
    NavigationView {
        List {
            TextField("Add Name", text: $parentViewModel.newListName)
            NavigationLink(destination: ChildView()) {
                Label("Select Products", systemImage: K.ListIcons.productsNr)
            }
        }
    }.environmentObject(parentViewModel)
}

The ChildView then references that environment Object via @EnvironmentObject without an initializer:

struct ChildView: View {

    @EnvironmentObject var parentViewModel: ParentViewModel

    var body: some View {
        NavigationView {
            List{
                Text("Some element")
                    .onTapGesture {
                        parentViewModel.alterData()
                    }      
            }
        }
    }
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Most likely you would use a binding for this situation:

struct ChildView: View {
    @Binding var name: String

    var body: some View {
        NavigationView {
            List{
                Text("Some element")
                    .onTapGesture {
                        name = "Altered!"
                    }      
            }
        }
    }
}

And in the parent:

struct ParentView: View {

  @StateObject var parentViewModel = ParentViewModel()

  var body: some View {
    NavigationView {
        List {
            TextField("Add Name", text: $parentViewModel.newListName)
            NavigationLink(destination: ChildView(name: $parentViewModel.newListName)) {
                Label("Select Products", systemImage: K.ListIcons.productsNr)
            }
        }
    }
}

Also, I think you can remove the NavigationView view from ChildView. Having it ParentView is enough.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda