ios – Altering the property of observableObject from one other observableObject


There’s a drawback of the next nature: it’s essential to create an authorization window for the applying, essentially the most logical resolution I discovered the next implementation (I had to do that as a result of the mainView has a tabView which behaves incorrectly whether it is in a navigationView)

struct ContentView: View {

@EnvironmentObject var vm: AppSettings

var physique: some View {
    if vm.isLogin {
    } else {

AppSettings appears like this:

class AppSettings: ObservableObject {
    @Printed var isLogin = false

By default, the person will probably be offered with an authorization window that appears like this:

struct LoginView: View {
    @StateObject var vm = LoginViewModel()
    var physique: some View {
        NavigationView {
            VStack {
                TextField("Electronic mail", textual content: $vm.login)
                TextField("Password", textual content: $vm.password)
                Button {
                } label: {
                    Textual content("SignIn")

And eventually the loginViewModel appears like this:

class LoginViewModel: ObservableObject {
    @Printed var login = ""
    @Printed var password = ""
  //@Printed var appSettings = AppSettings() -- error on the primary screenshot
  //@EnvironmentObject var appSettings: AppSettings -- error on the second screenshot

    func auth() {
        UserAPI().Auth(req: LoginRequest(electronic mail: login, password: password)) { response, error in
            if let err = error {
                // Error Processing
            } else if let response = response {
                Defaults.accessToken = response.tokens.accessToken
                Defaults.refreshToken = response.tokens.refreshToken
                self.appSettings.isLogin = true

1 error – Accessing StateObject’s object with out being put in on a View. This can create a brand new occasion every time

2 error – No ObservableObject of kind AppSettings discovered. A View.environmentObject(_:) for AppSettings could also be lacking as an ancestor of this view

I ask for assist, I simply can’t discover a method for the interplay of two observableObject. I needed to insert all of the logic into the motion of the button to implement such performance

Along with this performance, it’s deliberate to implement an exit from the account by altering the isLogin variable to false in numerous circumstances or use different setting variables to simply implement different features

The instance is intentionally simplified for a simple clarification of the scenario


Leave a Reply

Your email address will not be published. Required fields are marked *