1

I have the View 1 and View 2, View 1 has a button that navigate to View 2, in View 2 there is list of options to choose when clicked is expected to update a variable in View 1, but the variable in View 1 does not get updated.

View1.swift

struct View1: View {
    @ObservedObject var viewModel = MyViewModel()
    
    var body: some View {
        NavigationLink(destination: View2()) {
            VStack {
                Text(self.viewModel.addressChosen)
            }
        }
    }
}

View2.swift

struct View2: View {
    @ObservedObject var viewModel = MyViewModel()

    var body: some View {
        Button(action: {
            self.viewModel.chooseAddress(addressSelected: "Chosen Address")
        }) {
            VStack {
                Text(self.viewModel.addressChosen)
            }
        }
    }
}

MyViewModel.swift

class MyViewModel: ObservableObject, ModelService {
    @Published var addressChosen = "Default"

    func chooseAddress(addressSelected: String) {
        print(addressSelected)
        self.addressChosen = addressSelected
    }
}

The var addressChosen is updated in View 2, but when I go back to View 1, the variable does not get updated. What am I doing wrong? Thank you.

1 Answer 1

1

You are using two different MyViewModel. You need to pass the same MyViewModel() or only addressChosen from View1 to View2.

Pass the same MyViewModel() to View2

struct View1: View {
    @ObservedObject var viewModel = CartViewModel()
    
    var body: some View {
        NavigationLink(destination: View2(viewModel: viewModel)) {
            VStack {
                Text(self.viewModel.addressChosen)
            }
        }
    }
}

struct View2: View {
    @ObservedObject var viewModel: CartViewModel

    var body: some View {
        Button(action: {
            self.viewModel.chooseAddress(addressSelected: "Chosen Address")
        }) {
            VStack {
                Text(self.viewModel.addressChosen)
            }
        }
    }
}

class CartViewModel: ObservableObject {
    @Published var addressChosen = "Default"

    func chooseAddress(addressSelected: String) {
        print(addressSelected)
        self.addressChosen = addressSelected
    }
}

Pass addressChosen to View2

struct View1: View {
    @ObservedObject var viewModel = CartViewModel()
    
    var body: some View {
        NavigationLink(destination: View2(addressChosen: $viewModel.addressChosen)) {
            VStack {
                Text(self.viewModel.addressChosen)
            }
        }
    }
}

struct View2: View {
    @Binding var addressChosen: String

    var body: some View {
        Button(action: {
            self.addressChosen =  "Chosen Address"
        }) {
            VStack {
                Text(self.addressChosen)
            }
        }
    }
}

class CartViewModel: ObservableObject {
    @Published var addressChosen = "Default"

    func chooseAddress(addressSelected: String) {
        print(addressSelected)
        self.addressChosen = addressSelected
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thx for your answer, yes I'm actually using the same viewmodel, mistyped it on the question, editted it now. my codes is similar as your first answer with the same viewmodel, but the variable still not updating.
Oh i see, so I have to pass the viewmodel to the second view, not creating a new viewmodel, it does work now. Thank you very much sir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.